1

I am very new to all of this and I recently started learning JavaScript. To test my learning I made this simple script, Rock, paper, and scissors. It is something very similar to Codecademy project. The problem I am having is with the output, which comes out as 'undefined' and I can't figure out, what's giving this output, can someone please help?

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();

  if (userInput === 'rock') {
    return 'Rock' 
  } else if (userInput === 'paper') {
    return 'Paper' }
    else if (userInput === 'scissors') {
    return 'Scissors'} 
    else if (userInput === 'bomb') {
      return 'Bomb'
    } else {
        return 'Please input a valid choice!'
      }
      }

const getComputerChoice = () => {
  const numbers = (Math.floor(Math.random() * 3))

  switch (numbers) {
    case 0 : return "Rock";
    break;
    case 1 : return "Paper";
    break;
    case 2 : return "Scissors";
    break;
  } 
}

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
      return 'It\'s a tie!!';
    } 
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
       return 'The Computer has won the game!!';
    } else {
        return 'Congratulation You have won the game!!';
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return ('The Computer has won the game!!');
    } else {
       return ('Congratulations You have won the game!!');
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'paper') {
      return 'Cogratulations You have Won the game!!';
  } else {
      return 'The Computer has won the game!!';
  }
}
  if (userChoice === 'bomb') {
    return 'Congratulation you Won!!'
  }

};

const playGame = () => {
  var userChoice =  getUserChoice('rock')
  var computerChoice = getComputerChoice()
 console.log('You picked: ' + userChoice);
 console.log('The computer picked: ' +computerChoice)

  console.log(determineWinner(userChoice, computerChoice));
}
 playGame()

2
  • What is undefined? Please show your whole output. If the method does not reach a return, it will return undefined, btw Commented Aug 5, 2020 at 20:07
  • Hey, apparently someone figured it out. Thanks for the help though!! Commented Aug 5, 2020 at 20:20

4 Answers 4

2

Your userChoice and computerChoice are both capitalized. You are checking them against lowercase strings. Also, you're checking for scissors twice and not checking for paper.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();

  if (userInput === 'rock') {
    return 'Rock'
  } else if (userInput === 'paper') {
    return 'Paper'
  } else if (userInput === 'scissors') {
    return 'Scissors'
  } else if (userInput === 'bomb') {
    return 'Bomb'
  } else {
    return 'Please input a valid choice!'
  }
}

const getComputerChoice = () => {
  const numbers = (Math.floor(Math.random() * 3))

  switch (numbers) {
    case 0:
      return "Rock";
      break;
    case 1:
      return "Paper";
      break;
    case 2:
      return "Scissors";
      break;
  }
}

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'It\'s a tie!!';
  }
  if (userChoice === 'Rock') {
    if (computerChoice === 'Paper') {
      return 'The Computer has won the game!!';
    } else {
      return 'Congratulation You have won the game!!';
    }
  }
  if (userChoice === 'Paper') {
    if (computerChoice === 'Rock') {
      return ('The Computer has won the game!!');
    } else {
      return ('Congratulations You have won the game!!');
    }
  }
  if (userChoice === 'Scissors') {
    if (computerChoice === 'Paper') {
      return 'Cogratulations You have Won the game!!';
    } else {
      return 'The Computer has won the game!!';
    }
  }
  if (userChoice === 'Bomb') {
    return 'Congratulation you Won!!'
  }

};

const playGame = () => {
  var userChoice = getUserChoice('rock')
  var computerChoice = getComputerChoice()
  console.log('You picked: ' + userChoice);
  console.log('The computer picked: ' + computerChoice)

  console.log(determineWinner(userChoice, computerChoice));
}
playGame()

Sign up to request clarification or add additional context in comments.

Comments

0

You only check for Rock and Sciccors in your determine winner method

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();

  if (userInput === 'rock') {
    return 'Rock' 
  } else if (userInput === 'paper') {
    return 'Paper' }
    else if (userInput === 'scissors') {
    return 'Scissors'} 
    else if (userInput === 'bomb') {
      return 'Bomb'
    } else {
        return 'Please input a valid choice!'
      }
      }

const getComputerChoice = () => {
  const numbers = (Math.floor(Math.random() * 3))

  switch (numbers) {
    case 0 : return "Rock";
    break;
    case 1 : return "Paper";
    break;
    case 2 : return "Scissors";
    break;
  } 
}

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
      return 'It\'s a tie!!';
    } 
  if (userChoice === 'Rock') {
    if (computerChoice === 'Paper') {
       return 'The Computer has won the game!!';
    } else {
        return 'Congratulation You have won the game!!';
    }
  }
  if (userChoice === 'Scissors') {
    if (computerChoice === 'Rock') {
      return ('The Computer has won the game!!');
    } else {
       return ('Congratulations You have won the game!!');
    }
  }
  if (userChoice === 'Paper') {//You mean paper here
    if (computerChoice === 'Rock') {
      return 'Cogratulations You have Won the game!!';
  } else {
      return 'The Computer has won the game!!';
  }
}
  if (userChoice === 'bomb') {
    return 'Congratulation you Won!!'
  }

};

const playGame = () => {
  var userChoice =  getUserChoice('rock')
  var computerChoice = getComputerChoice()
 console.log('You picked: ' + userChoice);
 console.log('The computer picked: ' +computerChoice)

  console.log(determineWinner(userChoice, computerChoice));
}
 playGame()

Comments

0

The values you chose are capitalized, so, for example, you want to check if userChoice === 'Rock' instead of userChoice === 'rock'

Fixed:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();

  if (userInput === 'rock') {
    return 'Rock' 
  } else if (userInput === 'paper') {
    return 'Paper' }
    else if (userInput === 'scissors') {
    return 'Scissors'} 
    else if (userInput === 'bomb') {
      return 'Bomb'
    } else {
        return 'Please input a valid choice!'
      }
      }

const getComputerChoice = () => {
  const numbers = (Math.floor(Math.random() * 3))

  switch (numbers) {
    case 0 : return "Rock";
    break;
    case 1 : return "Paper";
    break;
    case 2 : return "Scissors";
    break;
  } 
}

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
      return 'It\'s a tie!!';
    } 
  if (userChoice === 'Rock') {
    if (computerChoice === 'Paper') {
       return 'The Computer has won the game!!';
    } else {
        return 'Congratulation You have won the game!!';
    }
  }
  if (userChoice === 'Scissors') {
    if (computerChoice === 'Rock') {
      return ('The Computer has won the game!!');
    } else {
       return ('Congratulations You have won the game!!');
    }
  }
  if (userChoice === 'Scissors') {
    if (computerChoice === 'Paper') {
      return 'Cogratulations You have Won the game!!';
  } else {
      return 'The Computer has won the game!!';
  }
}
  if (userChoice === 'Bomb') {
    return 'Congratulation you Won!!'
  }

};

const playGame = () => {
  var userChoice =  getUserChoice('rock')
  var computerChoice = getComputerChoice()
 console.log('You picked: ' + userChoice);
 console.log('The computer picked: ' +computerChoice)

  console.log(determineWinner(userChoice, computerChoice));
}
 playGame()

Comments

0

Welcome to programming then! Comment above is true - when you post, do try to be really specific about exactly what problem you're seeing. Makes it much easier to help you.

With that said though, I think from looking at the code above I can see what you mean. It's often helpful when debugging to walk through how your code will run:

  1. playGame() is called
  2. getUserChoice is called with the parameter 'rock'
  3. userChoice is assigned 'Rock' *note uppercase
  4. determineWinner is called with 'Rock' as userChoice
  5. 'Rock' does not trigger ANY of the if statements, and determineWinner does not return anything

So from following those steps its actually quite easy to see why determineWinner is undefined when you log it out... it doesn't return anything. The point of your getUserChoice function appears to be standardizing the inputs. But those standardized inputs are not being used properly later. You could consider storing those possible values in an array and then returning the lowercase value from that function?

Hope this helps - good luck!

1 Comment

Hey, thanks for the help and from next time I will make sure to keep this in mind before posting something :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.