我无法在不显示错误的情况下更改输入单词的大写。我用过 toLowerCase()

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'));

//这个好像不行。只有当我输入所有小写字母的单词时。

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase()
  
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    return 'Error!'; //changed to return
  }
};
const getUserChoice = (userInput=userInput.toLowerCase()) => {

在这一行中,您将创建函数参数的默认值,它将是 undefined,除非范围内存在 userInput。它不会操纵您传递的参数。

例如像这样的函数:

const hello = (name="stranger") => console.log(`hello, ${name}`)

如果不带参数调用,将说“你好,陌生人”,如果以 hello("Scrissors").

调用,将说“你好,剪刀”

当您调用该函数时:getUserChoice('Scissors') 默认参数被您传递的参数覆盖,函数中的 userInput"Scissors".

需要在函数体内进行转换:

const getUserChoice = (userInput) => {
   userInput=userInput.toLowerCase()
   /* now userInput is the lowercase version of the argument you pass */
}

你可以试试

const getUserChoice = (Input) => {
  let userInput = Input.toLowerCase();
  if (userInput==='rock' || userInput==='paper' || userInput==='scissors') {
    return userInput;
  } else {
    console.log('Error!');
  }
};
console.log(getUserChoice('Scissors'));