1

I'm learning Javascript and I make a rock, scissors, paper project. I already make it work with the sample code but now I want to use a function expression to use its value in other functions. When I load the code it gave me undefined and I don't know why. I assign all the values. Here is my code.

const userChoice = userInput => {
  //userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

//console.log(getUserChoice('Fork'));

const computerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors'
  }
};
//console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {
  console.log()
  if (userChoice === computerChoice) {
    return 'The game is a tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'bomb') {
    return 'You won!';

  }
}

const playGame = () => {

  console.log('You threw: ' + userChoice('rock'));
  console.log('The computer threw: ' + computerChoice());
  console.log(determineWinner(userChoice, computerChoice));
}
playGame();

Please help me.

Ele
  • 33,468
  • 7
  • 37
  • 75
  • So just to clarify, computerChoice = function() is not the same as having a function like function getComputerChoice() and then asign in to a varibele like let computerChoice = getComputerChoice()? Why? – Andres Pelaez 15 mins ago Delete –  May 30 '20 at 23:02

4 Answers4

0

You're passing the function userChoice as a param, you should store the result of calling the function userChoice and pass the result as a param in the function determineWinner

const userChoice = userInput => {
  //userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

//console.log(getUserChoice('Fork'));

const computerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors'
  }
};
//console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'The game is a tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'bomb') {
    return 'You won!';

  }
}

const playGame = () => {
  
  let uc = userChoice('rock');
  
  console.log('You threw: ' + uc);
  console.log('The computer threw: ' + computerChoice());
  console.log(determineWinner(uc, computerChoice));
}
playGame();
Ele
  • 33,468
  • 7
  • 37
  • 75
0

You should assign the outputs of userChoice and computerChoice to variables before passing them to determineWinner

const userChoice = userInput => {
  //userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

//console.log(getUserChoice('Fork'));

const computerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors'
  }
};
//console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {
  console.log()
  if (userChoice === computerChoice) {
    return 'The game is a tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'bomb') {
    return 'You won!';

  }
}

const playGame = () => {
  const userChose = userChoice('rock')
  const computerChose = computerChoice()
  console.log('You threw: ' + userChose);
  console.log('The computer threw: ' + computerChose);
  console.log(determineWinner(userChose, computerChose));
}
playGame();
richytong
  • 2,387
  • 1
  • 10
  • 21
  • So just to clarify, computerChoice = function() is not the same as having a function like function getComputerChoice() and then asign in to a varibele like let computerChoice = getComputerChoice()? Why? –  May 30 '20 at 22:46
  • `computerChoice = function() {...}` is the part where we declared the function. It is not the same as having a function like `getComputerChoice` and declaring a variable `let computerChoiceVariable = getComputerChoice()` – richytong May 31 '20 at 00:00
0

Try This

const userChoice = userInput => {
  //userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

//console.log(getUserChoice('Fork'));

const computerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors'
  }
};
//console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {
  console.log()
  if (userChoice === computerChoice) {
    return 'The game is a tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'bomb') {
    return 'You won!';

  }
}

const playGame = () => {

  console.log('You threw: ' + userChoice('rock'));
  console.log('The computer threw: ' + computerChoice());
  console.log(determineWinner(userChoice('rock'), computerChoice()));
}
playGame();
Bahtiyar
  • 192
  • 1
  • 6
0

just a few mistakes. You need to store the values into variables.

const userChoice = userInput => {
  //userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

//console.log(getUserChoice('Fork'));

const computerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors'
  }
};
//console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {
  console.log()
  if (userChoice === computerChoice) {
    return 'The game is a tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'bomb') {
    return 'You won!';

  }
}

const playGame = () => {
  const threw = {
      computer: null,
      user: null
  }
  threw.user = userChoice("rock")
  threw.computer = computerChoice()
  
  console.log('You threw: ' + threw.user);
  console.log('The computer threw: ' + threw.computer);
  console.log(determineWinner(userChoice(threw.user), threw.computer));
}
playGame();
Grampet
  • 177
  • 1
  • 1
  • 8