Rock Paper Scissors Project -- with prompt()

I have a quick question on the Rock Paper Scissors Project with prompt ()
https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript

Below is my code:

const getUserChoice = userInput => {
  userInput = prompt("Rock, Paper, or Scissors: ");
  userInput = userInput.toLowerCase();
  if (userInput === "rock" || userInput === "paper" || userInput === "scissors") {
    return userInput;
  } else {
    console.log("Please input one of the following: rock, paper, or scissors. ")
  }
};

getUserChoice();

I was just wondering that why is it that,

  1. I assigned the prompt value to userInput inside the getUserChoice function
  2. I didn’t put the following:
getUserChoice(userInput);

the code still works and runs properly?

I would’ve thought you need to do it this way:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === "rock" || userInput === "paper" || userInput === "scissors") {
    return userInput;
  } else {
    console.log("Please input one of the following: rock, paper, or scissors. ")
  }
};

let userInput = prompt("Rock, Paper, or Scissors: ");
getUserChoice(userInput);

Another question was,
is the parameter (userInput) declared in the function expression (const getUserChoice = userInput =>) a global/local/block scope?

I hope I made myself clear…? I am having a bit of a hard time explaining myself.

Thank you

Hello @ericasu0264443716, welcome to the forums!

This is because in JS, if a parameter is not given on function call (as in you just leave the brackets empty), it will automatically assign it the value undefined. While this does not throw an error, it can be quite bad for longer code.

This is local/function scope, as it will be accessible by everything in that (getUserChoice) function, but not outside it.

I hope this helps!

1 Like