Skip to content

Commit 41dbcdf

Browse files
committed
Merge pull request learn-co-students#3 from learn-co-curriculum/wip
Wip
2 parents e68a54d + c186f0c commit 41dbcdf

File tree

4 files changed

+109
-100
lines changed

4 files changed

+109
-100
lines changed

README.md

Lines changed: 38 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,30 @@
11
# JavaScript Flow Control
22

3-
## Overview
4-
5-
* About
6-
* If Statements
7-
* If/Else Statements
8-
* If/Else if/Else Statements
9-
* Ternary Operators
10-
* Switch Statements
11-
* Resources
3+
## Objectives
4+
+ Write if-statements in JS
5+
+ Write if-elsif-else statements in JS
6+
+ Use the ternary operator in JS
7+
+ Write switch statements in JS
128

139
## About
1410

15-
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`.
1614

1715
## If Statements
1816

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

21-
Syntax:
19+
JS Syntax:
2220

2321
```javascript
2422
if (conditionToTestIsTrue) {
2523
// code to be executed here
2624
}
2725
```
2826

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!"`
4028

4129
## If/Else Statements
4230

@@ -48,25 +36,13 @@ Syntax:
4836
if (conditionToTestIsTrue) {
4937
// condition is false hence code is not executed
5038
} else {
51-
// code to be executed because previous `if` condition is false
39+
// code to be executed because previous condition is false
5240
}
5341
```
5442

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"`.
5644

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
7046

7147
`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.
7248

@@ -78,27 +54,13 @@ Syntax:
7854
if (conditionToTestIsTrue){
7955
// condition is false hence code is not executed
8056
} else if (thisConditionIsTrue) {
81-
// execute this code if previous `if` statement is false
57+
// execute this code if previous statement is false
8258
} else {
8359
// execute this code if the 2 conditions above are false
8460
}
8561
```
8662

87-
Example:
88-
89-
```javascript
90-
var num = 10;
91-
92-
if (num < 10) {
93-
console.log(num + " is less than ten");
94-
} else if (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"`
10264

10365
## Ternary Operator
10466

@@ -121,13 +83,8 @@ Syntax:
12183
conditionToTest ? valueToBeReturnedIfTrue : valueToBeReturnedIfFalse
12284
```
12385

124-
Example:
86+
+ 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.
12587

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-
```
13188

13289
## Switch Statements
13390

@@ -147,52 +104,33 @@ switch (expression) {
147104
// code to be executed if case n and case m false
148105
}
149106
```
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:
175107

176-
```javascript
177-
var diet = "vegetarian";
108+
Example:
178109

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");
184118
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");
187127
}
128+
```
188129

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

193-
// Why????
194-
// Because break statement is found at the second case so execution stops there
195-
```
196134

197135
## Resources
198136

flow-control.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';

requires.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# A list of javascript files required for the project, in dependency order
2+
javascripts:
3+
files:
4+
- flow-control.js
5+
specs:
6+
- spec/flow-control-spec.js

spec/flow-control-spec.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
'use strict';
3+
describe('Flow Control Basics', function() {
4+
5+
describe('#basicTeenager', function() {
6+
it('should return "You are a teenager!" if the age is between 13-19', function() {
7+
expect(basicTeenager(13)).toBe("You are a teenager!");
8+
});
9+
10+
it('should return undefined if the age is not between 13-19', function(){
11+
expect(basicTeenager(12)).toBe(undefined);
12+
});
13+
14+
});
15+
16+
describe('#teenager', function() {
17+
it('should return "You are a teenager!" if the age is between 13-19', function() {
18+
expect(teenager(13)).toBe("You are a teenager!");
19+
});
20+
it('should return "you are not a teenager" if the age is not between 13-19', function(){
21+
expect(teenager(12)).toBe("You are not a teenager");
22+
expect(teenager(29)).toBe("You are not a teenager");
23+
});
24+
25+
});
26+
27+
describe('#ageChecker', function() {
28+
it('should return "You are a teenager!" if the age is between 13-19', function() {
29+
expect(ageChecker(13)).toBe("You are a teenager!");
30+
});
31+
it('should return "You are a kid" if the age is 12 or below', function(){
32+
expect(ageChecker(12)).toBe("You are a kid");
33+
});
34+
it('should return "You are a grownup" if the age is 20 or above', function(){
35+
expect(ageChecker(29)).toBe("You are a grownup");
36+
});
37+
38+
});
39+
40+
describe('#ternaryTeenager', function() {
41+
it('should return "You are a teenager" if age is between 13-19', function() {
42+
expect(ternaryTeenager(15)).toBe("You are a teenager");
43+
});
44+
45+
it('should return "You are not a teenager" if age not between 13-19', function() {
46+
expect(ternaryTeenager(75)).toBe("You are not a teenager");
47+
});
48+
49+
});
50+
51+
describe('#switchAge', function() {
52+
it('should return "You are a teenager" if age is between 13-19', function() {
53+
expect(switchAge(15)).toBe("You are a teenager");
54+
});
55+
56+
it('should return "You are not a teenager" if age not between 13-19', function() {
57+
expect(switchAge(75)).toBe("You have an age");
58+
expect(switchAge(7)).toBe("You have an age");
59+
});
60+
61+
});
62+
63+
64+
});

0 commit comments

Comments
 (0)