Practice 9C - COIN 70a

SWITCH

Food Quiz

Who was the most famous cook in America?

Thomas Jefferson
Julia Child
Emeril Lagasse
Nigella Lawson

What does the French term "creme brulé mean?

Beats me...
Put out the fire
Burnt Cream
I'll have cream in my coffee please.

Assignment:

Remember the assignment you did last week where you created a food quiz with
nested if...else if conditionals?? IF the answer to the question was "a" y
ou should do one thing, ELSE IF the answer was "b" you should do another thing...etc.
Revise that lab so that instead of using all those IF...ELSE IF's, you use a
Switch statement instead. Much easier!!

OLD CODE using IF..ELSE IF...ELSE

var inputName="";
function writeAlert(name) {
	inputName = name;
	if (inputName == "Thomas Jefferson") {
		alert("WRONG! He was a
		President!");
	}
	if (inputName == "Julia Child") {
		alert("Right you are!");
	}
	if (inputName == "Emeril Lagasse") {
		alert("Not yet!");
	}
	if (inputName == "Nigella Lawson") {
		alert("Who's that?");
	}
}
var frenchWord = "";
function writeIt(name) {
	frenchWord = name;
	if (frenchWord == "beats me") {
		alert("You should sign up
		for French!");
	} else if (frenchWord == "put out fire") {
		alert("No No No! You should
		sign up for French!");
	} else if (frenchWord == "burnt cream") {
		alert("Yes! And it tastes great!
		Mmmmmmmmm!");
	} else {
		alert("No - it has nothing
		to do with coffee!");
	}
}

NEW CODE using SWITCH

function writeAlert(name) {

	switch (name) {
		case "Thomas Jefferson":
		alert("WRONG! He was a President!");
		break;

		case "Julia Child":
		alert("Right you are!");
		break

		case "Emeril Lagasse":
		alert("Not yet!");
		break;

		case "Nigella Lawson":
		alert("Who's that?");
	}
}

function writeIt(name) {

	switch (name) {
		case "beats me":
		alert("You should sign up for French!");
		break;

		case "put out fire":
		alert("No No No! You should sign up for French!");
		break;

		case "burnt cream":
		alert("Yes! And it tastes great! Mmmmmmmmm!");
		break;

		case "coffee cream":
		alert("No - it has nothing to do with coffee!");
		break;
	}
}

BACK