Olá a todos,
Estou a preparar-me para iniciar um Bootcamp de JavaScript. Tenho algumas tarefas para fazer e, uma dela é esta que não estou a conseguir soluciona-la. Como tal, peso-vos ajuda!
Exercício:
Show me the code!
Remember the die roll simulator? Go ahead and transform the if - else if - else statements into a switch statement and check out how it gets easier to read.
var dieRoll = Math.ceil(Math.random() * 6);
if (dieRoll === 1) {
console.log(‘You roll a 1.’);
} else if (dieRoll === 2) {
console.log(‘You roll a 2.’);
} else if (dieRoll === 3) {
console.log(‘You roll a 3.’);
} else if (dieRoll === 4) {
console.log(‘You roll a 4.’);
} else if (dieRoll === 5) {
console.log(‘You roll a 5.’);
} else if (dieRoll === 6) {
console.log(‘You roll a 6.’);
} else {
console.log(‘This die only has 6 sides man…’);
}
A MINHA RESOLUÇÃO:
var dieRoll = Math.ceil(Math.random() * 6);
switch (dieRoll) {
case ‘You roll a 1.’:
console.log(‘You roll a 1.’);
break;
case ‘You roll a 2.’:
console.log(‘You roll a 2.’);
break;
case ‘You roll a 3.’:
console.log(‘You roll a 3.’);
break;
case ‘You roll a 4.’:
console.log(‘You roll a 4.’);
break;
case ‘You roll a 5.’:
console.log(‘You roll a 5.’);
break;
case ‘You roll a 6.’:
console.log(‘You roll a 6.’);
break;
default:
console.log(‘This die only has 6 sides man…’);
}
Output:
Code is incorrect
You should consider the 1 value in your switch
This die only has 6 sides man…
Agradeço a vossa ajuda!
