[undefined] What am I doing wrong?


#1

Hi,
I’ve created a code which works pretty fine, but in the end (result) I keep receiving ‘undefined’ message. I don’t know where it comes from.

Example:

You picked: scissors
The computer picked: rock
rock beats scissors
undefined

Next problem is that I cannot use propmt command. The interpreter says:

ReferenceError: prompt is not defined
at playGame

I tried to replace:
const userChoice = getUserChoice(‘scissors’);
with
const userChoice = prompt(‘Some info…’);

My code below:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if(userInput === 'paper' || userInput === 'scissors' || userInput === 'rock') {
    return userInput;
  } else if(userInput === 'bomb') {
    console.log('Secret weapon? I like it!');
    return userInput;
  } else {
    console.log('Wrong choice');
  }
}

const getComputerChoice = () => {
  switch(Math.floor(Math.random() * 3)) {
    case 0: 
      return 'paper';
      break;
    case 1: 
      return 'rock';
      break;
    case 2: 
      return 'scissors';
      break;
    default: 
      return 'What?';
  };
}

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    console.log('It\'s a tie!');
  } else if (userChoice === 'scissors' && computerChoice === 'rock') {
    console.log(`${computerChoice} beats ${userChoice}`);
  } else if (userChoice === 'scissors' && computerChoice === 'paper') {
    console.log(`${userChoice} beats ${computerChoice}`);
  } else if (userChoice === 'rock' && computerChoice === 'scissors') {
    console.log(`${computerChoice} beats ${userChoice}`);
  } else if (userChoice === 'rock' && computerChoice === 'paper') {
    console.log(`${userChoice} beats ${computerChoice}`);
  } else if (userChoice === 'paper' && computerChoice === 'rock') {
    console.log(`${computerChoice} beats ${userChoice}`);
  } else if (userChoice === 'paper' && computerChoice === 'scissors') {
    console.log(`${userChoice} beats ${computerChoice}`);
  } else if (userChoice === 'bomb') {
    console.log(`YOU WIN!!!`)
  }
}

const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log(`You picked: ${userChoice}`);
  console.log(`The computer picked: ${computerChoice}`);
  console.log(determineWinner(userChoice, computerChoice));
}

playGame();

EDIT: I figured out why the error is generated. Function determineWinner doesn’t have return command. It should look as per below:

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
     return 'It\'s a tie!';
  } else if (userChoice === 'scissors' && computerChoice === 'rock') {
    return `${computerChoice} beats ${userChoice}`;
  } else if (userChoice === 'scissors' && computerChoice === 'paper') {
    return `${userChoice} beats ${computerChoice}`;
......

Thanks for all explanations in advance.
Cheers!


#2

Always better to figure it out on your own, no more questions about this RPS program?


#3

Just one:
Why does this line result in an error?

const userChoice = prompt(‘Some info…’);
ReferenceError: prompt is not defined
at playGame

I am not familiar with defining prompt


#4

prompt() should be built-in, it should work fine.

i ran your code with prompt here:

http://jsbin.com/zidolaz/edit?js,console

and its fine


#5

Thanks a lot for taking care of this. Looks like it is not a problem with my code (methodology).
All problems solved. Have a good day!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.