Skip to content

Commit 66c6987

Browse files
committed
add resource count, tags, update switch content
1 parent 9f98bf2 commit 66c6987

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

.learn

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tags:
2+
- control flow
3+
- ternary operator
4+
- if statements
5+
- if/else statements
6+
- if/else if/else statements
7+
languages:
8+
- javascript
9+
resources: 4

README.md

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
## About
1414

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.
1616

1717
## If Statements
1818

@@ -68,7 +68,9 @@ if (num < 10) {
6868

6969
## If/Else if/Else Statements
7070

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`.
7274

7375
Syntax:
7476

@@ -100,12 +102,23 @@ if (num < 10) {
100102

101103
## Ternary Operator
102104

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:
104117

105118
Syntax:
106119

107120
```javascript
108-
conditionToTest ? valueToBeReturnedIfTrue : valueToBeReturnedIfFalse
121+
conditionToTest ? valueToBeReturnedIfTrue : valueToBeReturnedIfFalse
109122
```
110123

111124
Example:
@@ -135,12 +148,55 @@ switch (expression) {
135148
}
136149
```
137150

138-
Example:
151+
First Example:
139152

140153
```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
142195
```
143196

144197
## Resources
145198

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

Comments
 (0)