Reputation: 57
Here is the task:
In English, the colors of the rainbow are often listed as:
red orange yellow green blue indigo violet
Declare a function called
isRainbowColor
. Use case-insensitive checking, i.e."red"
,"Red"
,"RED"
,"ReD"
, and"rEd"
are all valid rainbow colors./** @param {string} ??? - the color to check @returns {boolean} whether or not the given color is a rainbow color */
Here is what I have so far (I know this is not enough!):
function isRainbowColor(string){
let rainbowColor = [];
for (const string of rainbowColor){
if (string != rainbowColor){
return true;
}else{
return false;
}
}
}
Here is the error message that I get:
Here is the test that I could not pass with my code above:
actual = isRainbowColor("red");
expected = true;
if (actual === expected) {
console.log("Test PASSED!");
} else {
console.error("Test FAILED. Keep trying!");
console.group("Result:");
console.log(" actual:", actual);
console.log("expected:", expected);
console.groupEnd();
}
actual = isRainbowColor("rEd");
expected = true;
if (actual === expected) {
console.log("Test PASSED!");
} else {
console.error("Test FAILED. Keep trying!");
console.group("Result:");
console.log(" actual:", actual);
console.log("expected:", expected);
console.groupEnd();
}
actual = isRainbowColor("Brown");
expected = false;
if (actual === expected) {
console.log("Test PASSED!");
} else {
console.error("Test FAILED. Keep trying!");
console.group("Result:");
console.log(" actual:", actual);
console.log("expected:", expected);
console.groupEnd();
}
Upvotes: 2
Views: 244
Reputation: 45
Here is the function isRainbowColor for your question :
So when using the function, you basically just have to just pass in a string as parameter (it doesn't matter if its uppercase, lowercase or mixed-case) and if it finds that string in the array "rainbowColors" then it will return true and if it doesn't finds that string in rainbowColors array, then it will return false
function isRainbowColor(color) {
var rainbowColors = ['violet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red'];
color = color.toString().toLowerCase();
rainbowColors.forEach(rainbowColor => {
if (color == rainbowColor) {
console.log(color + ' is a rainbow color');
return true;
}
});
return false;
}
Usage
-----------------------------------------------------------------
isRainbowColor('red');
Console : red is a rainbow color
true
-----------------------------------------------------------------
isRainbowColor('hi');
false
-----------------------------------------------------------------
Edit : Added support for all lowercase, uppercase and mixed-case string as the parameter value. Ex :
-----------------------------------------------------------------
isRainbowColor('RED');
red is a rainbow color
true
-----------------------------------------------------------------
isRainbowColor('RedddD');
false
-----------------------------------------------------------------
isRainbowColor('YeLlOW');
yellow is a rainbow color
true
-----------------------------------------------------------------
Upvotes: 1