Search code examples
javascriptstringprompt

Prompt not storing value as String


I'm working on an exercise but I'm not able to understand why my prompt is not being stored as a string and therefore can't be compared to my switch case

Any input returns false:

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

I noticed the input is not being stored as a String as I tried to .toUpperCase the "colorname" var and it won't change to Upper Case.


Solution

  • You are storing the result of getColor(colorname) in isAvailable variable and later instead of using that variable you are comparing the function name again.

    change your if from if (getColor === true) to if (isAvailable) or if (isAvailable === true)

    or one of these:

    if(getColor(colorname) === true)
    
    if(getColor(colorname))