Having trouble writing switch

Having trouble writing switch


Hi everyone. I’m working my way through the javascript lessons, but I’m having some trouble understanding how to write switch statements. This is a little different from the challenge thingy, but can you tell me how this is wrong? I’m trying to get my browser to spit out a colour:

function yocolour {
switch (colour) {
case "red":
document.write("red yo");
case "blue":
document.write("blue yo");
case "yellow":
document.write("yellow yo");


Your switch statement is fine. The problem is your function declaration and you call to the function.

Remember, functions look like:

function myFunction(optionalArgumentGoesHere) {
  // function code goes here

Also, if you want to send a parameter value of “red” to the function, you must use:



Thank you for taking the time to respond man, I really appreciate it :slight_smile: Quick question- can I send a predefined variable when calling the function, or do I have to put quotation marks around the data?


You can pass a variable to a function. In your example, if you would have defined a variable called red and assigned it the value “red”, then you could have called it as you originally did.

var red = "red"


Thanks! :slight_smile: