Alex
Alex

Reputation: 90

I can't change the capitalisation on the input word without an error showing up. I have used toLowerCase()

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

Answers (3)

vijay-navale
vijay-navale

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

Kasalwe
Kasalwe

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

pawel
pawel

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

Related Questions