"GetComputerChoice" part


#1

I’m stuck at this part for so long now, it keeps printing ‘Undefined’ everytime I run it.
My code is:

const getComputerChoice = () => {
let randomNumber = Math.random(Math.floor() * 3);
switch (randomNumber) {
case 0:
return “rock”;
break;
case 1:
return “scissors”;
break;
case 2:
return “paper”;
break;
};
}
console.log(getComputerChoice());

Can you please let me know why it doesn’t print rock, paper or scissors? Since it’s really annoying for me to be stuck and unable to identify what’s wrong, thank you in advance.


#2

lets look at the value of randomNumber:

let randomNumber = Math.random(Math.floor() * 3);
console.log(randomNumber);

seems something goes wrong when attempting to generate a random number, which is why not of the cases are true, and why you get undefined


#3

Could you please tell me how to fix it ? I finished the whole task and this part is still not working properly no matter how I try to modify it…


#4

the random number you generate:

Math.random(Math.floor() * 3);

isn’t correct, so you need to fix that. How did you try to modify it? You can always check documentation on MDN if you struggle with the math functions


#5

Well, I’ve just figured it out! It was such a naive mistake by me :’)
I inserted Math.floor inside of Math.random while i just had to swap their places to fix it, just wow :'D


#6

yes, very good. Figuring it out yourself is always better :slight_smile: Which is why i was trying to point you in that direction


#7

Thank you so much! Now I can finally get some rest :'D


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.