Rock, Paper

Why is it that when I run the console.log(randomNumber, getComputerChoice()); below (last line of my code), I receive an error stating that randomNumber is not defined?

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

console.log(randomNumber, getComputerChoice());

Hi,
because randomNumber is defined within your function and therefore not available in the global scope (outside the function).
Log randomNumber below its declaration, then you’ll see the result.

1 Like