You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Flow Control allows the execution of code only under certain conditions. In Ruby, we used case statements, if statements, if/else statements, if/elsif/else statements, ternary operators, and case statements to control what code runs when. JavaScript has similar methods to control what blocks of code to execute: if statements, if/else statements, if/else if/else statements, ternary operators, and switch statements.
11
+
Flow Control allows the execution of code only under certain conditions. In Ruby, we used if statements, if/else statements, if/elsif/else statements, ternary operators, and case statements to control what code runs when. JavaScript has similar methods to control what blocks of code to execute: if statements, if/else statements, if/else if/else statements, ternary operators, and switch statements.
12
+
13
+
You'll be writing your code in `js/flow-control.js`. Make sure to run the tests using `learn -b`.
16
14
17
15
## If Statements
18
16
19
-
Evaluates the code wrapped in parenthesis to either true or false. If `true`, the code block is executed:
17
+
An if statement evaluates the code wrapped in parenthesis to either true or false. If `true`, the code block is executed. If false, nothing is executed.
20
18
21
-
Syntax:
19
+
JS Syntax:
22
20
23
21
```javascript
24
22
if (conditionToTestIsTrue) {
25
23
// code to be executed here
26
24
}
27
25
```
28
26
29
-
Example:
30
-
31
-
```javascript
32
-
var num =7;
33
-
34
-
if (num <10) {
35
-
console.log(num +" is less than ten");
36
-
}
37
-
38
-
// This will print "7 is less than ten" to the console
39
-
```
27
+
Now, in `flow-control.js` let's write a function `basicTeenager` that accepts an age as a parameter. The function should contain an if-statement that checks to see if the age is a teenager. If the age is a teenager, it should return `"You are a teenager!"`
40
28
41
29
## If/Else Statements
42
30
@@ -48,25 +36,13 @@ Syntax:
48
36
if (conditionToTestIsTrue) {
49
37
// condition is false hence code is not executed
50
38
} else {
51
-
// code to be executed because previous `if` condition is false
39
+
// code to be executed because previous condition is false
52
40
}
53
41
```
54
42
55
-
Example:
43
+
+ Define a function `teenager` that accepts an age as a parameter. If the age is between 13-19 it should return `"You are a teenager!"`. Otherwise, the function should return `"You are not a teenager"`.
56
44
57
-
```javascript
58
-
var num =11;
59
-
60
-
if (num <10) {
61
-
console.log(num +" is less than ten");
62
-
} else {
63
-
console.log(num +" is more than or equal to ten");
64
-
}
65
-
66
-
// This will print "11 is more than or equal to ten" to the console
67
-
```
68
-
69
-
## If/Else if/Else Statements
45
+
## If/Else If Statements
70
46
71
47
`if` statements can also be combined with an `else if` clause. This is like an else statement, but with its own condition. It will only run if its condition is true, and the previous statement's condition was false.
72
48
@@ -78,27 +54,13 @@ Syntax:
78
54
if (conditionToTestIsTrue){
79
55
// condition is false hence code is not executed
80
56
} elseif (thisConditionIsTrue) {
81
-
// execute this code if previous `if` statement is false
57
+
// execute this code if previous statement is false
82
58
} else {
83
59
// execute this code if the 2 conditions above are false
84
60
}
85
61
```
86
62
87
-
Example:
88
-
89
-
```javascript
90
-
var num =10;
91
-
92
-
if (num <10) {
93
-
console.log(num +" is less than ten");
94
-
} elseif (num ==10) {
95
-
console.log(num +" is equal to ten");
96
-
} else {
97
-
console.log(num +" is more than ten");
98
-
}
99
-
100
-
// This will print "10 is equal to ten" to the console
101
-
```
63
+
+ Define a function `ageChecker` that takes in an age as a parameter. If the age is between 13-19 it should return `"You are a teenager"`. If the age is 12 or below, it should return `"You are a kid"`. If the age is above 19, it should return `"You are a grownup"`
+ Define a function `ternaryTeenager` that accepts age as a parameter. The body of the function should use the ternary operator to return `"You are a teenager"` if age is between 13-19 and returns `"You are not a teenager"` if the age is anything else.
125
87
126
-
```javascript
127
-
var num =7;
128
-
num >10?console.log(num +" is bigger than 10") :console.log(num +" is not bigger than 10");
129
-
// this will print "7 is not bigger than 10"
130
-
```
131
88
132
89
## Switch Statements
133
90
@@ -147,52 +104,33 @@ switch (expression) {
147
104
// code to be executed if case n and case m false
148
105
}
149
106
```
150
-
151
-
First Example:
152
-
153
-
```javascript
154
-
var diet ="vegetarian";
155
-
156
-
switch (diet) {
157
-
case"vegetarian":
158
-
console.log("Where do you get your protein? :P ");
159
-
case"gluten free":
160
-
console.log("Wow, that eliminates all the best foods! :( ");
161
-
default:
162
-
console.log("Sounds like you're an omnivore.");
163
-
}
164
-
165
-
// Output:
166
-
// Where do you get your protein? :P
167
-
// Wow, that eliminates all the best foods! :(
168
-
// Sounds like you're an omnivore.
169
-
170
-
// Why????
171
-
// Because no breaking statement is found it executes all of the cases after the true case (which is case 1)
172
-
```
173
-
174
-
Second Example:
175
107
176
-
```javascript
177
-
var diet ="vegetarian";
108
+
Example:
178
109
179
-
switch (diet) {
180
-
case"vegetarian":
181
-
console.log("Where do you get your protein? :P ");
182
-
case"gluten free":
183
-
console.log("Wow, that eliminates all the best foods! :( ");
110
+
```js
111
+
var mood ="hungry"
112
+
switch(mood){
113
+
case"happy":
114
+
console.log("Dance to Pharrel's Happy");
115
+
break;
116
+
case"sad":
117
+
console.log("You should eat a pint of icecream");
184
118
break;
185
-
default:
186
-
console.log("Sounds like you're an omnivore.");
119
+
case"anxious":
120
+
console.log("Take some deep breaths");
121
+
break;
122
+
case"hungry":
123
+
console.log("You should eat a big chocolate cake");
124
+
break;
125
+
default:
126
+
console.log("That's not a mood we support");
187
127
}
128
+
```
188
129
189
-
// Output:
190
-
// Where do you get your protein? :P
191
-
// Wow, that eliminates all the best foods! :(
130
+
In the example above, we'll see `"You should eat a big chocolate cake"` printed to the console. If we change the value of the `mood` variable to `sad` you'll see `"You should eat a pint of icecream"`. If the value of `mood` changed to `"grumpy"`, the default statement would trigger and print out `"That's not a mood we support"`.
131
+
132
+
+ Define a function `switchAge`that accepts an age as a parameter. The case statement should switch on `age` and return `"You are a teenager"` if the age is 13, 14, 15, 16, 17, 18, or 19, and return `"You have an age"` as the default.
192
133
193
-
// Why????
194
-
// Because break statement is found at the second case so execution stops there
0 commit comments