Rock, Paper, or Scissors JavaScript - Undefined not Error

console.log(“hi”);

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “paper” ||
userInput === “scissors”
) {
return userInput;
} else {
return “ERROR!!!”;
}
}

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

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return “The game is a tie!”;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’){
return “The computer Win”;
} else {return “Yeah!!! You Win”;}
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’){
return “The computer Win”;
} else {return “Yeah!!! You Win”;}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’){
return “The computer Win”;
} else {return “Yeah!!! You Win”;}
}
if(userChoice === ‘bomp’) {
return “Yeah!!! You Win”;
}
}
//console.log(determineWinner(‘scissors’,‘paper’));

const playGame = (userChoice= getUserChoice(),computerChoice= getComputerChoice()) => {
console.log(determineWinner(userChoice,computerChoice));
}

console.log(playGame(‘rock’,‘paper’));

//////////////////////////////////////////////////////
print out Undefined Code
output
=>

Output-only Terminal

Output:

hi
The computer Win
undefined
The computer Win
undefined

<=

help me please

Hi @werwipawee , if you use backticks to create a comment with code in the forum, it’s easier to read.
The sequence would be:

  1. Add three backticks, followed by the language: ```js
  2. Put in your code
  3. Close the code block with three more backticks: ```

Your code would look like this:

console.log(“hi”);

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (
    userInput === “rock” ||
    userInput === “paper” ||
    userInput === “scissors”
  ) {
    return userInput;
  } else {
    return “ERROR!!!”;
  }
}

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

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return “The game is a tie!”;
  }
  if (userChoice === ‘rock’) {
    if (computerChoice === ‘paper’){
      return “The computer Win”;
    } else {
      return “Yeah!!! You Win”;
    }
  }
  if (userChoice === ‘paper’) {
    if (computerChoice === ‘scissors’) {
      return “The computer Win”;
    } else {
      return “Yeah!!! You Win”;
    }
  }
  if (userChoice === ‘scissors’) {
    if (computerChoice === ‘rock’) {
      return “The computer Win”;
    } else {
      return “Yeah!!! You Win”;
    }
  }
  if(userChoice === ‘bomp’) {
    return “Yeah!!! You Win”;
  }
}
//console.log(determineWinner(‘scissors’,‘paper’));

const playGame = (userChoice= getUserChoice(),computerChoice= getComputerChoice()) => {
  console.log(determineWinner(userChoice,computerChoice));
}

console.log(playGame(‘rock’,‘paper’));

In terms of your solution, I would suggest some changes:

  1. Stick to one way to create strings, sometimes you use double quotes, sometimes backticks. Stick to one.
  2. In getUserChoice(), if the user types something different to “rock”, “paper”, “scissors”, it returns a string “ERROR!!”. So you need to check in determineWinner() if that is the case before attempting comparisons and decide what to do.
  3. In playGame() you’re trying to execute a function inside the params declaration of the function. It would be just fine if you only declare the params like (userChoice, computerChoice) => {}. Then, when you try to execute playGame(), you can use the functions you have for getting the values for each player:
const userChoice = getUserChoice("rock")
const computerChoice = getComputerChoice()
console.log(playGame(userChoice, computerChoice))

Hope this helps you!