Ask your own question, for FREE!
Computer Science 23 Online
OpenStudy (anonymous):

Another JavaScript question. Then this code: The value of rating is BBB. After the following code executes, what displays in the alter box? JavaScript: switch (rating){ case "AAA" : window.alert("Highest Quality"); break; case "AA" : window.alert("High Quality"); break; case "A" : window.alert("Upper Medium"); break; case "BBB" : window.alert("Medium "); break; case "BB" : window.alert("Speculative"); break; case "B", "CCC", "C" : window.alert("Highly Speculative "); break; default : window.alert("You did not enter a valid bond rating!"); break; }

OpenStudy (jagatuba):

Okay shiva. Switch/case statements are similar to the if/then in that they are looking for a condition. If the case is true then the code will execute, otherwise it will just continue on. Whatever variable that you name in the switch statement is the variable that will be up for comparison in the case statements. So in this instance, the variable being compared is rating. Based on this information, which case statement do you think will execute?

OpenStudy (anonymous):

case "B", "CCC", "C" :

OpenStudy (jagatuba):

No. The value of rating is BBB, so case"BBB" would execute, meaning that the alert box would display "Medium ".

OpenStudy (anonymous):

Shiva, In all C-style languages, a switch statement takes the whole value of the variable into account. For instance, if I were to write this code (where variableNameHere = "something" earlier in the code): ... switch(variableNameHere){ case "some": window.alert("Value is 'some'!"); break; case "thing": window.alert("Value is 'thing'!"); break; case "something": window.alert("Value is 'something'"); break; case "anything", "else": window.alert("I think you gave me something else."); break; default: window.alert("You didn't give me anything :/"); break; } The alert would show "Value is "something'!" because that was the only full, exact match. It does not look only at the first or last segments of the switch variable's value or do a partial match - it takes the whole darn thing. Now, if variableNameHere had the value "anything" or "else", it would go into the 2nd to last case. If it had the value "anythingelse" or "anything else", however, it would fall into the default case.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!