Rock, Paper, Scissors Project Help (undefined error)

Hey Guys!

I have been having an undefined error show up (in relation to the Rock, Paper, Scissors JavaScript project) after I save my code after completing 13/14 tasks. I’ve been reading the code over again for the past few hours but no luck so far. This project has been tough but a good learning experience. I am also new to coding and JavaScript so still trying to learn how it operates. So, I was wondering if anyone else could help me figure out what the issues are. Thank you in advance!

The ‘undefined’ error:
2
You threw: scissors
The computer threw: undefined ← Error here
You won!

My code:
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “paper” ||
userInput === “scissors”
) {
return userInput;
} else {
console.log(“Error please, type: rock, paper, or scissors”);
}
};
const getComputerChoice = (computerInput) => {
const randomNumber = console.log(Math.floor(Math.random() * 3));
switch (randomNumber){
case 0:
return “rock”;
case 1:
return “paper”;
case 2:
return “scissors”;
}
};

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return “Tie game!”;
}

if (userChoice === “rock”) {
if (computerChoice === “paper”) {
return “The computer won!”;
} else {
return “You won!”;
}
}

if (userChoice === “paper”) {
if (computerChoice === “scissors”) {
return “The computer won!”;
} else {
return “You won!”;
}
}

if (userChoice === “scissors”) {
if (computerChoice === “rock”) {
return “The computer won!”;
} else {
return “You won!”;
}
}
};

const playGame = () => {
let userChoice = getUserChoice(‘scissors’);
let computerChoice = getComputerChoice();
console.log('You threw: ’ + userChoice);
console.log('The computer threw: ’ + computerChoice);
console.log(determineWinner(userChoice,computerChoice));
}

playGame();

Please format your code to make it easier to read and debug.
Also specify which variable is undefined, please.

I apologize for not formatting the previous code. I hope it is formatted correctly now.

The ‘formatted’ code:
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “paper” ||
userInput === “scissors”
) {
return userInput;
} else {

    console.log("Error please, type: rock, paper, or scissors");
  }
};
const getComputerChoice = (computerInput) => {
  const randomNumber = console.log(Math.floor(Math.random() * 3));
  switch (randomNumber){
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";
  }
};

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return "Tie game!";
  }

if (userChoice === "rock") {
  if (computerChoice === "paper") {
    return "The computer won!";
  } else {
    return "You won!";
  }
}

if (userChoice === "paper") {
  if (computerChoice === "scissors") {
    return "The computer won!";
  } else {
    return "You won!";
  }
}

if (userChoice === "scissors") {
  if (computerChoice === "rock") {
    return "The computer won!";
  } else {
    return "You won!";
  }
}
};

const playGame = () => {
    let userChoice = getUserChoice('scissors');
    let computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
    console.log('The computer threw: ' + computerChoice); <-[**The variable being undefined**]
  		console.log(determineWinner(userChoice,computerChoice));
}
 
playGame();

const randomNumber = console.log(Math.floor(Math.random() * 3));
The variable randomNumber is undefined because console.log() returns undefined.

4 Likes

That solved the undefined variable issue! I must have forgotten to change the console log function to the switch function. Thank you!

1 Like