Why isn't this code working? Please help

Why isn't this code working? Please help
0

#1

Here is the following code I have done for a Codecademy course project:

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

  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

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



const determineWinner = (userChoice, computerChoice) => {
  if(userChoice === computerChoice) {
    return 'The game was a tie!';
  } else if(userChoice === 'rock' && computerChoice === 'paper') {
    return 'The computer won the game';
  } else {
    return 'You have won the game!';
  }
  
  if(userChoice === 'paper' && computerChoice === 'scissors') {
    return 'The computer won the game';
  } else {
    return 'You have won the game!';
  }
  
  if(userChoice === 'scissors' && computerChoice === 'rock') {
    return 'The computer has won the game';
  } else {
    return 'You have won the game!';
  }
}
console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('paper', 'paper'));
console.log(determineWinner('scissors', 'rock'));

There are strings logging to the console when I run the code, but just not the right ones.

What is it about my if statements in the determineWinner function would make the return not print? For example, when I run this the only block that calls correctly is “‘The game is a tie!’”. The other ones just log “‘You have won the game!’”. I don’t understand what I did wrong here.

Also, please excuse me if I I’m not explaining myself correctly with all of the correct terms regarding JS. I’m still learning, and welcome correction for any terms missused (i.e., call, log, function, etc)! :slight_smile:


#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

When a return statement is executed, the function exits and does not come back to finish any other code. In the case of the user having “paper” and the computer having “scissors”, in the first console.log statement, the following if/else if/else code block is the only thing that will be evaluated.

  if(userChoice === computerChoice) {
    return 'The game was a tie!';
  } else if(userChoice === 'rock' && computerChoice === 'paper') {
    return 'The computer won the game';
  } else {
    return 'You have won the game!';
  }
  

Why? Because the first if statement evaluates to false and the else if evaluates to false, so the else block of code executes, so you return ‘You have won the game!’, when in reality, the next set of if/else if/else statements should be allowed to run. You are going to need to adjust your logic.


#3
const determineWinner = (userChoice, computerChoice) => {
  if(userChoice === computerChoice) {
    return 'The game was a tie!';
  } else if(userChoice === 'rock') {
    if (computerChoice === 'paper') {
    return 'The computer won the game';
  } else {
    return 'You have won!';
  } 
  }

Ah… hm, okay. So, then would this adjustment to the logic allow the other if blocks to run?


#4

It seems that code would allow other blocks to run. The best way to know for sure, is test it.

FYI - There is a much easier way to solve this challenge without all those if/else if/else statements. When you get your function to work, post your code and I will show a more simplified version that only has one if statement.


#5

I just did! It worked :slight_smile: thank you for your help with the code and the note about forum post readability!