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