提示不将值存储为字符串

Prompt not storing value as String

我正在做一个练习,但我无法理解为什么我的提示没有存储为字符串,因此无法与我的 switch case 进行比较

任意输入returns错误:

function getColor(selection) {
  switch (selection) {
    case 'red':
      return true;
    case 'green':
      return true;
    case 'blue':
      return true;
    default:
      return false; //returns false because the user picked an unavailable color
  }
}

var colorname = prompt('What color do you want?');
var isAvailable = getColor(colorname);

if (getColor === true) {
  console.log('Good news! That color is available');
} else {
  console.log('We are sorry, that color is not available');
}

我注意到当我尝试 .toUpperCase “colorname” var 时,输入没有存储为字符串,它不会更改为大写。

您将 getColor(colorname) 的结果存储在 isAvailable 变量中,稍后您不再使用该变量而是再次比较函数名称。

将您的条件从 if (getColor === true) 更改为 if (isAvailable)if (isAvailable === true)

或其中之一:

if(getColor(colorname) === true)

if(getColor(colorname))