Project Rock, Paper, Scissors

Hello dear coder,

I’m new to programming, but am determined to learn it… so please help me understand my error…
In this project ( https://www.codecademy.com/courses/learn-javascript/projects/rock-paper-scissors?action=resume_content_item ) I did everything what was written in the tasks. If I was unsure I used the hint, corrected my errors etc.

Now I finished it and it’s just kind of a mess… and I don’t know why. Sometimes the correct winning condition shows, but most of the time it doesn’t.

Here’s the code:

var userInput = "Rock";
userInput = userInput.toLowerCase();

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

function determineWinner() {
  var userChoice = userInput;
  var computerChoice = getComputerChoice();
  if (userChoice === computerChoice) {
  	return "Game is a tie!";  
  }
  if (userChoice === "rock") {
    if (computerChoice === "paper") {
      return "Computer wins!"; 
    } else {
      return "User wins!";
    }
  }
  if (userChoice === "paper") {
    if (computerChoice === "scissors") {
      return "Computer wins!";
    } else {
      return "User wins!";
    }
  }
  if (userChoice === "scissors") {
    if (computerChoice === "rock") {
      return "Computer wins!";
    } else {
      return "User wins!"
    }
  }
}

function playGame() {
  var userChoice = userInput;
  var computerChoice = getComputerChoice();
  console.log("You threw: " + userChoice);
  console.log("Computer threw: " + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}

playGame()

What I don’t understand: without creating the var for both userChoice and computerChoice in the function determineWinner, it gives me a syntax error although in the lesson’s tasks, nothing is written in there…

Thanks in advance.

OK, I found my error.
both var in playGame are wrong, I set them above the function and deleted them in the function determineWinner… now everything is correct.

This is how it looks low:

var userInput = "Paper";
userInput = userInput.toLowerCase();

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

function determineWinner() {
  if (userChoice === computerChoice) {
  	return "Game is a tie!";  
  }
  if (userChoice === "rock") {
    if (computerChoice === "paper") {
      return "Computer wins!"; 
    } else {
      return "User wins!";
    }
  }
  if (userChoice === "paper") {
    if (computerChoice === "scissors") {
      return "Computer wins!";
    } else {
      return "User wins!";
    }
  }
  if (userChoice === "scissors") {
    if (computerChoice === "rock") {
      return "Computer wins!";
    } else {
      return "User wins!"
    }
  }
}

var userChoice = userInput;
var computerChoice = getComputerChoice();

function playGame() {
  console.log("You threw: " + userChoice);
  console.log("Computer threw: " + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}

playGame()

Ok, that works, but it’s still kind of messy :slight_smile:


We use functions to encapsulate some logic, this allows us to use the same code many times without having to copy it. So take a look at this function:

function addTwoNumbers() {
  return a + b;
}

This function is similar to determineWinner. If I would like to use this function two times to calculate 5 + 7 and 13 + 42 I would have to write this code:

function addTwoNumbers() {
  return a + b;
}

var a, b;

a = 5;
b = 7;
console.log(addTwoNumbers());

a = 13;
b = 42;
console.log(addTwoNumbers());

So I have to remember that used variables are named a and b and when I copy this function to another project I have to make sure that I do not use a and b variables already in my code (because they would be overwritten). In technical language this is called global scope pollution. A lot of effort!


This is better approach:

function addTwoNumbers(a, b) {
  return a + b;
}

var x = 5;
var y = 7;
console.log(addTwoNumbers(x, y));

// maybe I only need to use 13 and 42 in this single
// function call, so I do not create variables
console.log(addTwoNumbers(13, 42));

Here my function takes two parameters and I can freely use variables named a and b in other parts of my code.


So get back to previous version of your code, delete these lines:

var userChoice = userInput;
var computerChoice = getComputerChoice();

from body of the determineWinner function. And try to pass userChoice and computerChoice as parameters to your function. Figure this out, you are really close to the correct solution! :slight_smile:

Hello and thanks for your reply!

To be honest: exactly THIS was confusing me for the first time while I went through the tasks. Why declaring different variables for the same thing, but it’s clearly written in the tasks.

Task 1: Declare a variable named userInput
Task 3: Declare a new function named getComputerChoice
Task 4: Declare a function named determineWinner that takes two parameters named userChoice and computerChoice .

Then later at Task 8 (the part why I was unsure and wrote in here): Inside the playGame function, create a variable named userChoice set equal to userInput .
Then, create another variable named computerChoice set equal to the function call of the getComputerChoice function.

I don’t know why exactly it should be inside (because it can’t work) and I don’t know why 2x2 var, but my guess is for training/excercise purpose and to see how things work (to be honest, I do now a little bit better).

You’ll probably see more of me from now on. =) Have a nice day.