Reputation: 90
const getUserChoice = (userInput=userInput.toLowerCase()) => {
if (userInput==='rock'||userInput==='paper'||userInput==='scissors') {
return userInput;
}
else {
console.log('Error!');
}
};
console.log(getUserChoice('Scissors'));
//This does not seem to work. Only if I type in the words in all lowercase letters.
Upvotes: 2
Views: 108
Reputation: 1
You can try doing
const getUserChoice = (Input) => {
let userInput = Input.toLowerCase();
if (userInput==='rock' || userInput==='paper' || userInput==='scissors') {
return userInput;
} else {
console.log('Error!');
}
};
console.log(getUserChoice('Scissors'));
Upvotes: 0
Reputation: 368
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase()
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
return 'Error!'; //changed to return
}
};
Upvotes: 4
Reputation: 36965
const getUserChoice = (userInput=userInput.toLowerCase()) => {
In this line you create the default value of the function argument, which will be undefined
unless userInput
exists in the scope. It doesn't manipulate the argument you pass.
E.g. a function like this:
const hello = (name="stranger") => console.log(`hello, ${name}`)
will say "hello, stranger" if called with no arguments, and "hello, Scissors" if called as hello("Scrissors")
.
When you call the function: getUserChoice('Scissors')
the default argument is overriden by what you have passed, and userInput
inside your function is "Scissors"
.
You need to do the conversion inside the function body:
const getUserChoice = (userInput) => {
userInput=userInput.toLowerCase()
/* now userInput is the lowercase version of the argument you pass */
}
Upvotes: 2