Create a new function named getComputerChoice with no parameters. Inside its block, utilize Math.random() and Math.floor() to get a whole number between 0 and 2. Then, depending on the number, return either 'rock' , 'paper' , or 'scissors' .
so I wrote this code:
function getComputerChoice() {
const randomNumber = Math.floor(Math.random()*3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return : 'scisscors';
};
}
I got error. The project walkthrough shows that he used function expression not this âfunction declarationâ but I donât understand why this is wrong?
@nicolahearn I think what stetim94 was trying to get at is that sometimes we can find whatâs wrong with our code just by examining the behavior of the program and the outputs we are getting back.
Especially as beginners, we shouldnât be shy to add console.log() whenever we feel like we need to check that variables are holding the values we expected. Basically, getting good at debugging is super useful!
undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.
You were getting undefined because your function wasnât returning anything, so JS was giving it the value of undefined! Awesome, right?