#Super Beginner Question: Checking Input Variable...

11 messages · Page 1 of 1 (latest)

outer dagger
#

Hello! I am working on my first beginner project with JS and am doing a rock, paper, scissors problem.

I am stuck on something so basic that it is frustrating (so apologies for that).

I am trying to figure out how I can print what has been 'user input' within my code (just to confirm), but can't seem to figure out this basic thing. Does it have to do with scope?

The console.log at the bottom throws me an error that userInput is not defined.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock'){
    console.log('Time to smash');
    return userInput;
  } else if (userInput === 'paper'){
    console.log('You are gonna fold');
    return userInput;
  } else if (userInput === 'scissors'){
    console.log('You better not run...');
    return userInput;
  } else {
    console.log('You need to pick a valid choice yall!');
  };
};

getUserChoice('scissors');
console.log(userInput);
#

I guess this is also a general question 'does anyone have any tips on keeping track of variable assignment for learning/debugging purposes?'

olive arch
#

userInput is only defined within the scope of getUserChoice. Your console log at the end is outside of the function.

outer dagger
#

The second from bottom line calls that function with 'scissors' as the userInput - does that then get wiped from memory by the next line?

olive arch
torpid idol
olive arch
torpid idol
#

meh, I suppose the async section isn't super helpful. this is more about scope like Choo has said.

outer dagger
#

gotcha, thank you for the resources - I just finished a 101 and am trying to do some light projects, so still very light on experience as you can see. I am used to data manipulation tools so I think the unfamiliarity with variables having or not having values at any given point is throwing me off a bit.

#

I think I am expecting a persistent floating variable table and that isn't how it works.

olive arch