2

How can I get the following function to return multiple color types (not just red, but also orange, yellow, etc.) without using multiple if/else statements?

function isRainbowColor(color){
  if (color === "red"){
  return true;
  }else{
    return false;
  }
}
console.log(isRainbowColor("red"));
 //This returns true, but only for one color at a time.  How can I add onto ?
Tristach
  • 21
  • 3

1 Answers1

2

Create an array of acceptable colors, and check the color with the Array.includes() function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

function isRainbowColor(color){
  let acceptableColors = ["red","orange","yellow","blue","green"]
  if (acceptableColors.includes(color)){
  return true;
  }else{
    return false;
  }
}
console.log(isRainbowColor("red"));
console.log(isRainbowColor("green"));
console.log(isRainbowColor("purple"));
 //This returns true, but only for one color at a time.  How can I add onto ?
symlink
  • 11,984
  • 7
  • 29
  • 50