So I am doing the rock, paper scissor challenge in Javascript and in the video lesson the person ends up using a switch statement for the getComputerChoice function. He writes it similar to this with no breaks…
const getComputerChoice = () => {
const randomNumber = (Math.floor(Math.random() * 3));
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
};
We learned to write it like this, with breaks…
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
I think I understand why there is not default, since you only have 3 choices and no need for the default. Is it the same with the breaks that would normally be there? There is no worry for the code to error out since it is generating 0,1, or 2? Is that best practice or should the breaks always be there? Just trying to understand the syntax and reason why.
Thanks