Rock paper scissors project

The below code block I am using to generate a random number, and then assign a value of either rock, paper, or scissors as part of a game.

However, when logging this to the console, rather than outputting the assigned values to the numbers of rock, paper, or scissors, it pastes the original number, ignoring the switch statement I have created. Why is this?

Thank you.

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’) {
return userInput
} else {
return (‘No valid input detected!’)

const getComputerChoice = () => {
const randomNumber = console.log(Math.floor(Math.random() * 3));
switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;


You wrote:

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

In the above statement, you are picking a random integer in the range 0-2, printing it to the console, and then you are assigning the value returned by the console.log() method to the variable randomNumber. But the console.log() method’s return value is undefined (See documentation). The console.log() method doesn’t return what it printed, it just returns undefined. That is just how the method is implemented in JavaScript. You can confirm this by adding the statement,

const randomNumber = console.log(Math.floor(Math.random() * 3));
console.log(randomNumber) // undefined

Since randomNumber ends up being undefined, so none of the switch cases match and you exit the getComputerChoice function without explicitly returning any value. Therefore, the function implicitly returns undefined which is then printed by the statement console.log(getComputerChoice());

What you should do instead is:

const randomNumber = Math.floor(Math.random() * 3);

Now you will generate a random number in the range 0-2 and assign it to the variable randomNumber. You can then print this number if so inclined to check whether your code is working as expected.


Thank you… worked perfectly!

1 Like