Having trouble writing switch

Having trouble writing switch
0

#1

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");
break;
case "blue":
document.write("blue yo");
break;
case "yellow":
document.write("yellow yo");
}
}
yocolour(red);

#2

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:

myFunction("red");

#3

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?


#4

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"
yocolour(red);

#5

Thanks! :slight_smile: