Please help: Error states that Math.random is not defined

Please help: Error states that Math.random is not defined
0

#1

Please help: Error states that Math.random is not defined

function getComputerChoice() {
 var randomNumberbetween0and2 = Math.floor(Math.random() * 2);
  
  return Math.random();
}

switch (randomNumber) {
  case 0:
    console.log('rock');
    break;
  case 1:
    console.log('paper');
    break;
  case 2: 
    console.log('scissors');
    break;
  default: console.log('Invalid entry');
    break;
                    }

#2

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

Please link to your CodePen or project repository as more context may be necessary to help you.


#3

What is your idea with getComputerChoice()? It currently returns a random number between 0 and 1 and it doesn’t do anything with the randomNumberBetween0and2.


#4

Hey @BenGitter you almost there you need to replace the return Math.random() with variable randomNumberbetween0and2 and call it on switch statement. Maybe it is good idea to increase the number 2 by 1 in your function getComputerChoice()because it is upto 2 and not including it. Here I managed to get desired output https://jsfiddle.net/kfk4dsd0/


#5

Thank you @asanhix. It worked!


#6

@StormyRamsey thanks and happy coding.