|
12 | 12 |
|
13 | 13 | ## About |
14 | 14 |
|
15 | | -Flow Control allows the excution of code only under certain conditions. |
| 15 | +Flow Control allows the excution of code only under certain conditions. In Ruby, we used case statments, if statments, 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. |
16 | 16 |
|
17 | 17 | ## If Statements |
18 | 18 |
|
@@ -68,7 +68,9 @@ if (num < 10) { |
68 | 68 |
|
69 | 69 | ## If/Else if/Else Statements |
70 | 70 |
|
71 | | -`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. |
| 71 | +`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 | + |
| 73 | +Note: An important thing to keep in mind when transitioning to JavaScript from Ruby is that the `elsif` becomes two full words: `else if`. |
72 | 74 |
|
73 | 75 | Syntax: |
74 | 76 |
|
@@ -100,12 +102,23 @@ if (num < 10) { |
100 | 102 |
|
101 | 103 | ## Ternary Operator |
102 | 104 |
|
103 | | -The ternary operator is used as a shortcut for the `if-else` statement. This operator tests a condition; if the condition is true, it returns a certain value, otherwise it returns a different value: |
| 105 | +The ternary operator is used as a shortcut for the `if-else` statement. You've probably seen it before in Ruby looking something like this: |
| 106 | + |
| 107 | +```ruby |
| 108 | +cart = ["graphic t-shirt", "aluminum water bottle"] |
| 109 | + |
| 110 | +puts cart.empty? ? "Please add something to your cart." : "You're ready to check out." |
| 111 | + |
| 112 | +# Above prints: |
| 113 | +# You're ready to check out. |
| 114 | +``` |
| 115 | + |
| 116 | +This operator tests a condition; if the condition is true, it returns a certain value, otherwise it returns a different value: |
104 | 117 |
|
105 | 118 | Syntax: |
106 | 119 |
|
107 | 120 | ```javascript |
108 | | - conditionToTest ? valueToBeReturnedIfTrue : valueToBeReturnedIfFalse |
| 121 | +conditionToTest ? valueToBeReturnedIfTrue : valueToBeReturnedIfFalse |
109 | 122 | ``` |
110 | 123 |
|
111 | 124 | Example: |
@@ -135,12 +148,55 @@ switch (expression) { |
135 | 148 | } |
136 | 149 | ``` |
137 | 150 |
|
138 | | -Example: |
| 151 | +First Example: |
139 | 152 |
|
140 | 153 | ```javascript |
141 | | -// todo |
| 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 | + |
| 176 | +```javascript |
| 177 | +var diet = "vegetarian"; |
| 178 | + |
| 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! :( "); |
| 184 | + break; |
| 185 | + default: |
| 186 | + console.log("Sounds like you're an omnivore."); |
| 187 | +} |
| 188 | + |
| 189 | +// Output: |
| 190 | +// Where do you get your protein? :P |
| 191 | +// Wow, that eliminates all the best foods! :( |
| 192 | + |
| 193 | +// Why???? |
| 194 | +// Because break statement is found at the second case so execution stops there |
142 | 195 | ``` |
143 | 196 |
|
144 | 197 | ## Resources |
145 | 198 |
|
146 | | -* []() |
| 199 | +* [Codecademy - if/if else/if else if else](http://www.codecademy.com/glossary/javascript/if-statement) |
| 200 | +* [MDN - if..else](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) |
| 201 | +* [Codecademy - Ternary Operator](http://www.codecademy.com/glossary/javascript/ternary-operator) |
| 202 | +* [Codecademy - Switch Statements](http://www.codecademy.com/glossary/javascript/switch-statements) |
0 commit comments