Help with Javascript issue

getColor Conditional Exercise

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:
  1. if phrase is ‘stop’, you should print out ‘red’

  2. if phrase is ‘slow’, you should print out ‘yellow’

  3. if phrase is ‘go’, you should print out ‘green’

  4. 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!’.

Your output

red purple

© 2021 Udemy, Inc.

Hi @JoeShmo94 and welcome back :wave:

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:

} else {
  console.log("purple");
}

I hope that helps.
Have a nice day!

Michael

1 Like

Perfect! That worked. Thank you so much for the help!

What did you type? It’s not working for me.

Hi @Lahad_Israel and welcome :wave:

If you use @JoeShmo94’s code and apply my correction. it should work.
Could you show your code?

Thanks and cheers
Michael

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 {
console.log(‘purple’);
}
//AND THIS LINE ↑↑↑↑↑
}

You are missing the quotes around 'stop' and 'slow'.

I hope this fixes your issue :slightly_smiling_face:
Michael

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

HI @Kristina_Kultysheva and welcome to the community :wave:

You wrote consol.log. This should be console.log.
I think the rest is correct.

Cheers,
Michael

Thanks a lot!!!:smiley:

Чт, 30 июня 2022 г. в 12:19, Michael Koch via Mozilla Discourse <notifications@discourse.mozilla.org>:

1 Like

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 {
console.log(“purple”);
}

Hello all, thanks in advance:) Cannot get what is the problem, my code seems to look right but solution is saying wrong!

Hi @Nazgul_Zhuzumova and welcome to the community :wave:

It seems you are missing the curly brace } that closes the getColor(phrase) function at the very end of your pasted code. The last } is from the else.

Cheers,
Michael

1 Like

Thanks a bunch! my code is working now :slight_smile:

1 Like