Replacing integer with string value - Rock, Paper, Scissors project

Hi everyone,

I’m currently working through the following exercises. Stuck in the first half where I need to assign rock, paper or scissors to a randomly generated 0, 1 or 2 value.

I’ve tried two ways. First one (attempting to) using switch/case, however this one just logs blank or nothing to the console. I’ve tried running it through another console online and it returned an Illegal return statement.

function numGenerator () {
  return Math.floor(Math.random()*3);
}

switch (numGenerator) {
  case 0:
  return console.log("rock");
  break;
  case 1:
  return console.log("paper");
  break;
  case 3:
  return console.log("scissors");
  break;
}

The second option was using if / else, however when I test it in the console it sometimes returns “rock”, “paper” or “scissors” and sometimes returns 0, 1 or 2.

function getComputerChoice () {
  if(Math.round(Math.random()*2) === 0) {
    return "rock";
  } else if (Math.round(Math.random()*2) === 1) {
    return "paper";
  } else if (Math.round(Math.random()*2) === 2) {
    return "scissors";
  } else {
    return Math.round(Math.random()*2)
  }
}

If a kind soul can help me out with this, you would have my eternal internet stranger gratitude <3

These returns will be undefined since the string isn’t returned.

Your answer made me go back to the lesson and review the theory. Got it sorted. Thanks for the nudge!

1 Like