Learn Javascript Rock Paper Scissors Project

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!
https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript

I am doing the rock paper scissors project and I am experiencing quite a bit of difficulty. For some reason my code is saying that the userChoice is not defined. Can anyone take a look at it?

Learn JavaScript | Codecademy

Hey Stikeouts27,

You did not declared userChoice . You declared getUserChoice on line 1; however, you never declared userChoice. Let me know if that resolves your issue!

I am still having issues with my code. I am having trouble reading the Javascript error messages. I do not see in which line there is an issue with the code.

The error message reads:
/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:68
});
^
SyntaxError: Unexpected token )
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Can someone help me decipher this? And point out what is wrong?

You’re missing two curly braces from your determineWinner function.

The next problem you’ll run into is your getComputerChoice returning undefined values. Look at the value you are assigning to randomNumber. Are you assigning a random number to randomNumber or something else?

To answer your question about the error message more specifically, only the first line is relevant to your code. I don’t know exactly why it’s telling you that there’s an unexpected ) when the problem is two missing } but this kind of error message generally means you need to go back and check that you’ve closed all of your brackets and braces correctly.

The error messages below the first line relate to Codecademy’s coding environment itself. There’s a load of complex code that goes into rendering a text editor and terminal in a browser and those error messages are about all that stuff, not what you’ve typed inside the text editor.

1 Like

I believe that math.random assigns a random value between 0-0.99. I than have it multiplied by 3 and than have it floored so that it always results in a number of 0,1 or 2. However I am getting undefined values and I do not know why. Or rather the code I have in front of me appears to be correct. I look at the inputs of the determine winner function and observe that I am still placing strings of “paper” “scissors” and “rock” which I think defeats the purpose of having a random number. Can anyone advise me on how to diagnose this problem so that I can solve it? Such as, what questions I should be asking myself?

const userChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “scissors” ||
userInput === “paper”
) {
return userInput;
} else {
console.log(“Error, please type: rock, paper or scissors.”);
}
};

const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);

switch (randomNumber) {
case 0:
return “rock”;
case 1:
return “paper”;
case 2:
return “scissors”;
}
};

console.log(getComputerChoice());
console.log(getComputerChoice());
console.log(getComputerChoice());

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return “This game is a tie!”;
}
};
if (userChoice === “rock”) {
if (computerChoice === “paper”) {
return “sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}

if (userChoice === “paper”) {
if (computerChoice === “scissors”) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}

if (userChoice === “scissors”) {
if (computerChoice === “paper”) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}

// task 7
// when the text is not colored normally that is a sign of what is wrong.
// if you see a red ending bracket it is most likely an extra one you do not need.
// you do not need to have parenthesis around the return

// task 8
// we already covered what happens in a tie in the last task. in this task we say if the player chooses rock, than there are only two other scenarios. one in which the computer picks paper, in which case, the comptuer wins. and the ohter where the computer picks scissors, and the player wins. the else statement is for scissors and doesn’t need to be fully written out becuase it is the only option left.

I have made the edits that you have suggested and it works better. But now I am getting an undefined error message. I percieve that the math.random function generates a number between 0-0.99 which I multiply by three and than floor to make it where it only gets 0,1,2.

I feel like the issue is in my switch statement. I am still inputting computer choices instead of having the computer generate a random number.

The issue was that you were assigning your variable randomNumber to the return value of a console.log() statement, rather than a number. The return value of a console.log() statement is always undefined. It looks like you have fixed this error though.

If you’re still getting errors, feel free to post another Hastebin link to your current code and I’ll have a look.

I appreciate everyones help! I decided to look at the get unstuck video which codecademy provides and I tried copying the code as closlely as I could. I belive that the video presented has errors in the code that either I am not detecing or the code that is provided in the solution video is wrong. I have lost confidence in that video and its solution and am hoping to do a cusotmized solution.

here is my code. It is saying that userChoice is not defined. and it is saying play game function is not working.

Hopefully I am making the changes that people are suggesting.

Again I appreciate everyones help.

If you haven’t fixed this yet, then it is a real problem since it closes the function body before the if conditionals.

const userChoice = userInput => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “scissors” ||
userInput === “paper” ||
userInput === ‘bomb’
) {
return userInput;
} else {
console.log(“Error, please type: rock, paper or scissors.”);
}

if (userChoice == ‘bomb’) {
return ‘Congratulations, you won!’
}
}

const computerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);

switch (randomNumber) {
case 0:
return “rock”;
case 1:
return “paper”;
case 2:
return “scissors”;
}
};

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return “This game is a tie!”;
};

if (userChoice === “rock”) {
if (computerChoice === “paper”) {
return “sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}

if (userChoice === “paper”) {
if (computerChoice === “scissors”) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}

if (userChoice === “scissors”) {
if (computerChoice === “paper”) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
if(userChoice === ‘bomb’) {
return ‘Congratulations, you won!’;
}
};

const playGame = playGame = () => {
const userChoice = getUserChoice(‘paper’);
const computerChoice = getComputerChoice();
console.log('You threw: ’ + userChoice);
console.log('The computer threw: ’ + computerChoice);

console.log(determineWinner(userChoice, computerChoice));
};

playGame()

// task 7
// when the text is not colored normally that is a sign of what is wrong.
// if you see a red ending bracket it is most likely an extra one you do not need.
// you do not need to have parenthesis around the return

// task 8
// we already covered what happens in a tie in the last task. in this task we say if the player chooses rock, than there are only two other scenarios. one in which the computer picks paper, in which case, the comptuer wins. and the ohter where the computer picks scissors, and the player wins. the else statement is for scissors and doesn’t need to be fully written out because it is the only option left.

If you can still edit your post, select all the code and click the </> button in the tool tray.

Looks like the determinewinner function is not closed.

const userChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "scissors" ||
    userInput === "paper" ||
    userInput === 'bomb'
  ) {
    return userInput;
  } else {
    console.log("Error, please type: rock, paper or scissors.");
  }

  if (userChoice == 'bomb') {
    return 'Congratulations, you won!'
  }
}

const computerChoice = () => {
  const randomNumber = Math.floor(Math.random() * 3);

  switch (randomNumber) {
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";
  }
};


const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice)
    return "This game is a tie!";
};

if (userChoice === "rock") {
  if (computerChoice === "paper") {
    return "sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "paper") {
  if (computerChoice === "scissors") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "scissors") {
  if (computerChoice === "paper") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  if(userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
};

const playGame = playGame = () => {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

  console.log(determineWinner(userChoice, computerChoice)); 
}; 

playGame()

Few things:

  • Drop all the semi-colons after the closing braces; they are superfluous.
  • Do a thorough count of the braces and be sure they are correctly placed.
  • Switch to the standard function and drop the arrow syntax.
1 Like

Like this?

const function userChoice(userInput) = {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "scissors" ||
    userInput === "paper" ||
    userInput === 'bomb'
  ) {
    return userInput;
  } else {
    console.log("Error, please type: rock, paper or scissors.");
  }

  if (userChoice == 'bomb') {
    return 'Congratulations, you won!'
  }
}

const computerChoice {
  const randomNumber = Math.floor(Math.random() * 3);

  switch (randomNumber) {
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";
};

const function determineWinner = (userChoice, computerChoice)
{
  if (userChoice === computerChoice)
    return "This game is a tie!";
}

if (userChoice === "rock") {
  if (computerChoice === "paper") {
    return "sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "paper") {
  if (computerChoice === "scissors") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "scissors") {
  if (computerChoice === "paper") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  if(userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
}

const function = playGame() {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

  console.log(determineWinner(userChoice, computerChoice)); 
} 

playGame()

Not quite. Function expression goes like,

const userChoice = function (userInput) {
    //  lots of code
    //  may include explicit return
}

My reasoning for this choice is that I reserve arrow syntax for truly expressive functions, callbacks, and such where the function body is a concise and brief expression that can be immediately returned. For something like this project the standard syntax is a better fit since there is so much code in each function, and I like the way it reads (without syntactic sugar spewn into the mix).

Preferred use of arrow syntax:

              /  expression  \
const addAB = (a, b) => a + b;
                           \
                        expression
const userChoice = function (userInput) = {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "scissors" ||
    userInput === "paper" ||
    userInput === 'bomb'
  ) {
    return userInput;
  } else {
    console.log("Error, please type: rock, paper or scissors.");
  }

  if (userChoice == 'bomb') {
    return 'Congratulations, you won!'
  }
}

const computerChoice function() = {
  const randomNumber = Math.floor(Math.random() * 3);

  switch (randomNumber) {
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";
};

const determineWinner function(userChoice, computerChoice)
{
  if (userChoice === computerChoice)
    return "This game is a tie!";
}

if (userChoice === "rock") {
  if (computerChoice === "paper") {
    return "sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "paper") {
  if (computerChoice === "scissors") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "scissors") {
  if (computerChoice === "paper") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  if(userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
}

const playgame function() {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

  console.log(determineWinner(userChoice, computerChoice)); 
} 

playGame()

error code:
/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:1
(function (exports, require, module, __filename, __dirname) { const userChoice = function (userInput) = {
^
SyntaxError: Unexpected token =
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

We will only need the one assignment.

//       reference       function
//        assignment    expression
                \          /
const userChoice = function (userInput) {

}

P. S.
We’re hanging with you, so give this your all. Time is not a factor. Especially glad that posting code samples is becoming clear. Kudos.

const userChoice = function (userInput)  {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "scissors" ||
    userInput === "paper" ||
    userInput === 'bomb'
  ) {
    return userInput;
  } else {
    console.log("Error, please type: rock, paper or scissors.");
  }

  if (userChoice == 'bomb') {
    return 'Congratulations, you won!'
  }
}

const computerChoice function() = {
  const randomNumber = Math.floor(Math.random() * 3);

  switch (randomNumber) {
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";
};

const determineWinner function(userChoice, computerChoice)
{
  if (userChoice === computerChoice)
    return "This game is a tie!";
}

if (userChoice === "rock") {
  if (computerChoice === "paper") {
    return "sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "paper") {
  if (computerChoice === "scissors") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "scissors") {
  if (computerChoice === "paper") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  if(userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
}

const playgame function() {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

  console.log(determineWinner(userChoice, computerChoice)); 
} 

playGame()

okay what should I do next?

Missing closing brace.

Missing opening brace.