Why is my If else not working in rock paper scissors


#1

hi just can’t seem to figure out why my code is not working. it seemed to be working before and I don’t think I changed anything…

<> const getUserChoise =userInput =>
userInput = userInput.toLowerCase();
let getComputerChoise
Math.floor(Math.random() * 3);

if(userInput === ‘rock’ || userInput
=== ‘paper’ || userInput === ‘scissors’ ){
return userInput;
} else {
console.log(‘Error!’);
} </>

ReferenceError: userInput is not defined
at Object


#2

Please use the </> symbol after highlighting the code portion of you post and then format the code with the same indentations as the original code. If you can also include a copy of the error, it would help in resolving this.


#4

Is this the entire program?


#5

Yes thats all… not sure where I went wrong


#6

Sorry, for the long wait but I try not to post on weekends. The error that you are receiving is because you don’t have userInput defined in the global scope where you are referencing it in the if statement. You should define a variable called userInput using let.

//the userInput in the function here, is a parameter
const getUserChoise =userInput => userInput = userInput.toLowerCase();
let getComputerChoice = () => Math.floor(Math.random() * 3);

//the userInput in the if statement is a variable and needs to be declared 
//before using

let userInput = getUserChoise('some string'); //<---- ****this line is important

if(userInput === ‘rock’ || userInput
=== ‘paper’ || userInput === ‘scissors’ ){
return userInput; //<------****also, don't use return unless you are writing a function, use console.log()
} else {
console.log(‘Error!’);
} 

This will get the code here to work.


#7

thanks for the help!


#8

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