Different Switch Statement "Initial Evaluations"


#1

(Or whatever you call this part

...

switch (theVariable) {  // THIS PART HERE, THIS FIRST LINE
	case "something":

...

that serves as the initial, well, “evaluation” for “running” the whole switch-thingamajig!)

So my question is: when do we use switch (aVariable) and switch (Boolean)??

I ask 'cause of stuff I’m doing that seems to suggest that we use the former, a variable name, in situations like case 5 and the latter, a Boolean value (usually true in my experience so far [can anyone confirm this for themselves, too?]), for case (val2 < 0)…is this understanding correct?

THANKS!!


Can't Use "+=" in Chrome Console?!?!
#2

switch is a query object which means all it wants is a binary resolution. How we phrase it is up us.

 switch (true) {
 case expression: break;
 case expression: break;
 case expression: break;
 case expression: break;
 default: false;
}
 switch (expression) {
 case value; break;
 case value: break;
 case value: break;
 case value: break;
 default: false;
}

#3

Hmmm…

Okay, let me lay it on ya, then:

1.

function yoNumma () {

	var numma = parseInt(prompt("gimme a numma"));

	switch (true) {
		case (numma < 0):
			alert("No Negative Nummmaz");
			break;
		case (numma % Math.sqrt(numma) === 0 && numma !==1):
			alert("Perfect Square! " + "(" + parseInt(Math.pow(Math.sqrt(numma), 2)) + " = " + Math.sqrt(numma) + " * " + Math.sqrt(numma) + ")");
			break;
		case (numma % 2 !== 0):
			alert("The numma is 'Odd!'");
			break;
		default:
			alert(numma + " is a nice numma. ^_^");
	}
}

yoNumma();

2.

var userInput = prompt("What would you like to do?\n\nType:\n\"New\" to add a To-Do\n\"List\" to view the To-Do List\n\"Quit\" to exit this program");

switch (userInput) {
	case "new":
		alert("newing!");
		break;
	case "list":
		alert("listing!");
		break;
	case "quit":
		alert("quiting!");
		break;
	default:
		userInput = prompt("Please choose \"New,\" \"List,\" or \"Quit:\"");
}

Why must I use true for 1 and not numma? Why must I use userInput for 2 and not true??

I’m sorry but while what you said sounds interesting, I’m unable to apply it to my puzzlement…


#4

A query is like a needle-haystack search. It needs a match to succeed. The first expression to be true will be the branch taken, in 1, and the first value to match the variable will be the branch taken, in 2.