Let’s get some practice with conditional statements! In order to make your code repeatable and testable, I’m asking you to write your code inside a pre-defined function (something we have not yet covered in the course).
Write your code between the two comments in index.js
You will automatically have access to a variable called phrase . Please do not try and define phrase or change phrase in any way! I will be setting the value of phrase when I test your code.
Your job is to print out a color based upon the following rules:
if phrase is ‘stop’, you should print out ‘red’
if phrase is ‘slow’, you should print out ‘yellow’
if phrase is ‘go’, you should print out ‘green’
if phrase is anything else, you should print out ‘purple’
function getColor(phrase){
//WRITE YOUR CODE BETWEEN THIS LINE: ↓ ↓ ↓ ↓
if (phrase === “stop”){
console.log(“red”);
} else if (phrase === “slow”){
console.log(“yellow”);
} else if (phrase == “go”){
console.log(“green”);
} else (phrase == “hello”); {
console.log(“purple”);
}
//AND THIS LINE ↑↑↑↑↑
}
Oops, your solution is incorrect.
when phrase is “stop” Expected 2 to be 1, ‘expected ONE thing to be console logged!’.
You have to delete (phrase === “hello”);. The else at the end should never have a condition. Because of the semicolon after this part the conditional is stopped and console.log("purple"); gets always called. It should just read:
Please, help))))I always have this error "when phrase is “stop” " in my code:
function getColor(phrase){
if (phrase === “stop”){
consol.log(“red”);
} else if (phrase === “slow”){
consol.log(“yellow”);
} else if (phrase === “go”){
consol.log(“green”);
}else {
consol.log(“purple”);
}
}