Home > Mobile >  Uncaught SyntaxError: "Unexpected token )" | If else statement in JavaScript
Uncaught SyntaxError: "Unexpected token )" | If else statement in JavaScript

Time:06-11

It seems my if else condition has a syntax error, can somebody help? The console spits out this link { /home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:38 }); } and an error points to the ")"

  userInput = userInput.toLowerCase();
  if (userInput !== "rock" && userInput !== "paper" && userInput !== "scissor") {
    console.log("Error");
  } else {
    return userInput;
  }
}

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

const determinWinner = (userChoice, computerChoice) => {
  if(userChoice === computerChoice){
    return "Its a tie";
  } else if (userChoice === "rock" && computerChoice ==="scissor"){
    return "The user won";
  } else if (userChoice === "paper" && computerChoice ==="rock"){
    return "The user won";
  } else if (userChoice === "scissor" && computerChoice ==="paper"){
    return "The user won";
  } else if (userChoice === "rock" && computerChoice ==="paper"){
    return "The computer won";
  } else if (userChoice === "paper" && computerChoice ==="scissor"){
    return "The computer won";
  } else if (userChoice === "scissor" && computerChoice ==="rock"){
    return "The computer won";
  }

CodePudding user response:

You are just missing the last/closing } for your determinWinner(...) method.

This is how you code should look like!

const determinWinner = (userChoice, computerChoice) => {
  if(userChoice === computerChoice){
    return "Its a tie";
  } else if (userChoice === "rock" && computerChoice ==="scissor"){
    return "The user won";
  } else if (userChoice === "paper" && computerChoice ==="rock"){
    return "The user won";
  } else if (userChoice === "scissor" && computerChoice ==="paper"){
    return "The user won";
  } else if (userChoice === "rock" && computerChoice ==="paper"){
    return "The computer won";
  } else if (userChoice === "paper" && computerChoice ==="scissor"){
    return "The computer won";
  } else if (userChoice === "scissor" && computerChoice ==="rock"){
    return "The computer won";
  }
}

CodePudding user response:

You defining an enclosure, but you're missing the final }

  const determinWinner = (userChoice, computerChoice) => {
  if(userChoice === computerChoice){
    return "Its a tie";
  } else if (userChoice === "rock" && computerChoice ==="scissor"){
    return "The user won";
  } else if (userChoice === "paper" && computerChoice ==="rock"){
    return "The user won";
  } else if (userChoice === "scissor" && computerChoice ==="paper"){
    return "The user won";
  } else if (userChoice === "rock" && computerChoice ==="paper"){
    return "The computer won";
  } else if (userChoice === "paper" && computerChoice ==="scissor"){
    return "The computer won";
  } else if (userChoice === "scissor" && computerChoice ==="rock"){
    return "The computer won";
  }
}

CodePudding user response:

Try this: At the end of computerChoice){, add : and see if that works!

  • Related