Basic Javascript 2 #4

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is a switch statement?

A switch statement tests a value and can have many case statements which define various possible values. Statements are executed from the first matched case value until a break is encountered.

Should a default statement be last?

Yes.

Write a switch statement which tests val and sets answer for the following conditions: 1 - "alpha" 2 - "beta" 3 - "gamma" 4 - "delta"

function caseInSwitch(val) { var answer = ""; // Only change code below this line switch(val){ case 1 : answer = "alpha"; break; case 2 : answer = "beta"; break; case 3 : answer = "gamma"; break; case 4 : answer = "delta"; break; }

Change the chained if/else if statements into a switch statement. function chainToSwitch(val) { var answer = ""; if (val === "bob") { answer = "Marley"; } else if (val === 42) { answer = "The Answer"; } else if (val === 1) { answer = "There is no #1"; } else if (val === 99) { answer = "Missed me by this much!"; } else if (val === 7) { answer = "Ate Nine"; }

function chainToSwitch(val) { var answer = ""; switch (val) { case "bob": answer = "Marley"; break; case 42: answer = "The Answer"; break; case 1: answer = "There is no #1"; break; case 99: answer = "Missed me by this much!"; break; case 7: answer = "Ate Nine"; break; }

Fix the function isLess to remove the if/else statements function isLess(a, b) { if (a < b) { return true; } else { return false; } }

function isLess(a, b) { return a < b; }

Write a switch statement to set answer for the following ranges :1-3 - "Low" 4-6 - "Mid" 7-9 - "High"

function sequentialSizes(val) { var answer = ""; switch (val) { case 1: case 2: case 3: answer = "Low"; break; case 4: case 5: case 6: answer = "Mid"; break; case 7: case 8: case 9: answer = "High"; break; }

Write a switch statement to set answer for the following conditions: "a" - "apple" "b" - "bird" "c" - "cat" default - "stuff"

function switchOfStuff(val) { var answer = ""; switch (val) { case "a": answer = "apple"; break; case "b": answer = "bird"; break; case "c": answer = "cat"; break; default: answer = "stuff"; break; }


Set pelajaran terkait

State exam simulator- weighted exam

View Set

Chapter 4: Retirement and Other Insurance Concepts

View Set

Endocrine Practice Questions, Endocrine

View Set

Chapter 11 Personal Finance: Investing Basics and Evaluating Bonds

View Set

Accident Causation and Investigation

View Set

[Lección 4] Contextos 3 - ¿Qué son?

View Set