Using Switch Statements Flashcards

1
Q

What are Switch Statements?

A

They are used inlace of If/Else to write more efficient / condensed code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the 4 parts to a switch statement?

A
Switch = tells JS that you're about to list conditions. 
Case =  think of "incase". States the parameter, and then is followed by an alert. 
Break; = Prevents fall through ( code continuing through  even after a parameter was met). Use it after every case
Default = if user gives an undefined parameter, it is the catch all.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you start writing a switch?

A
if (grade === "regular") {
alert("Its $3.35");
}
BECOMES
switch (grade) {
case "regular":
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you write a case?

A
if (grade === "Regular") {
alert("Its $3.35"):
}
becomes 
switch (grade) {
case "regular":
alert("its $3.35"):
       break;
case "premium":
alert("its $4.50"):
       break;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Do you use colons or semicolons after every case?

A

you use colons. :

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Does break; go before or after alert() ?

A

after. you break your code after each alert you present.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give an example of default:

A

break:
alert(“that is not valid grade”):
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What punctuation do you use after each: break, case, switch, default

A

break; (semi)
case: (colon)
switch (var) {
default:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is fall through?

A

Code automatically wants to go to the next line of code regardless if a condition is met. That is fall through. you prevent it with break;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a sample code for 3 types of fruit and make sure to have a default.
BONUS = Turn it into a function.

A

… You have fun with that one :D …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly