Project Issue - Rock Paper Scissors - "Undefined"

When I used the following code and log console.log(getComputerChoice()); - a correct value is logged to the console, and then I also get “undefined”. Can someone tell me why I’m also getting an undefined value?

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

Hello, and welcome to the forums!

console.log() doesn’t return a value, so it will always be undefined if you try to return it. You should be returning the string itself. The correct value being logged to the console is only because the function itself is also logging, but it’s only returning undefined.

You could prove this by testing:

const computerChoice = getComputerChoice();
console.log('The computer move is ', computerChoice);

With your current code, it would print The computer move is undefined since that’s what your function is returning.

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

const computerChoice = getComputerChoice();
console.log('The computer move is ', computerChoice);

The computer move is undefined

So the soluttion is a rubbish.

So how to get rid of this undefined? All was done according to Learn Javascript: Rock, Paper, Scissors - YouTube

so what should be done to get right answer?

Hello @array8831187478 , welcome to the forums! As you may notice, in the video, they use integers in the case statements (which is what Math.Floor(Math.random()*3) produces):

switch (randomNumber) {
  case 0:
  //code
}
//etc.

Look carefully at the 0. Then look at your 0. Are they the same?

Hint

You’ll notice you’re 0 has quotation marks "" around it. That means it is a String.