Rock Paper Scissors question: Why use the userInput variable?


#1

I just have a quick question about the Rock Paper Scissors exercise. The solution presented in the lesson requires the use of a variable called var userInput and later, in the playGame() function, that variable is set to equal another variable called userChoice.

I’ve found that the program works just as well if we forget about var userInput altogether. Below is my code for the program:

var userChoice = "rock";
userChoice = userChoice.toLowerCase();

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 "It's a tie!"
  }
  if (userChoice === "rock") {
    if (computerChoice === "paper") {
      return "paper beats rock. You lose."
    }
    else {
      return "rock beats scissors.  You win."
    }
  }
  if (userChoice === "paper") {
    if (computerChoice === "scissors") {
      return "scissors beats paper.  You lose."
    }
    else {
      return "paper beats rock.  You win."
    }
  }
  if (userChoice === "scissors") {
    if (computerChoice === "rock") {
      return "rock beats scissors.  You lose."
    }
    else {
      return "scissors beats paper.  You win."
    }
  }
  if (userChoice === "dynamite") {
    return "BOOM!! You win."
  }
}

function playGame() {
  var computerChoice = getComputerChoice();
  
  console.log(userChoice);
  console.log(computerChoice);
  
  console.log(determineWinner(userChoice, computerChoice));
}

playGame()

This works fine, regardless of whether the user chooses rock, paper, or scissors. I was just wondering what the point of the var userInput variable was. It doesn’t seem to add anything and (to me, at least) makes the exercise unnecessarily confusing. Am I missing anything?

Many thanks.


#2

userInput is a variable, wherease userChoice is a parameter. A parameter is a placeholder, a variable is a value assigned to a keyword. Using the names of variables you have defined as parameters is not advisable, as it may cause errors.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.