Hi everyone,
I’m currently working through the following exercises. Stuck in the first half where I need to assign rock, paper or scissors to a randomly generated 0, 1 or 2 value.
I’ve tried two ways. First one (attempting to) using switch/case, however this one just logs blank or nothing to the console. I’ve tried running it through another console online and it returned an Illegal return statement.
function numGenerator () {
return Math.floor(Math.random()*3);
}
switch (numGenerator) {
case 0:
return console.log("rock");
break;
case 1:
return console.log("paper");
break;
case 3:
return console.log("scissors");
break;
}
The second option was using if / else, however when I test it in the console it sometimes returns “rock”, “paper” or “scissors” and sometimes returns 0, 1 or 2.
function getComputerChoice () {
if(Math.round(Math.random()*2) === 0) {
return "rock";
} else if (Math.round(Math.random()*2) === 1) {
return "paper";
} else if (Math.round(Math.random()*2) === 2) {
return "scissors";
} else {
return Math.round(Math.random()*2)
}
}
If a kind soul can help me out with this, you would have my eternal internet stranger gratitude <3