samedi 9 novembre 2019

Declaration or statement expected in If Statements

So I am working on some code to build a simple application in Javascript where the computer has a random number assigned to rock, paper, scissors and choose randomly and then has a userchoice as well. The code is not running not sure why.

I have tried adding semicolon to the end of the if statements that are inside the main if statement in the determineWinner function.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  }else {
    console.log('Error, you must type rock, paper, or scissors');
  }
}

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

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

console.log(determineWinner('paper', 'scissors'));

When running the console.log at the end of the script it should display that the computer has won.

Aucun commentaire:

Enregistrer un commentaire