Skip to content

Commit ed61848

Browse files
committed
Add more explanation, add debugger, use expect
Fixes #13 (?)
1 parent b6b6a83 commit ed61848

File tree

5 files changed

+120
-35
lines changed

5 files changed

+120
-35
lines changed

README.md

+70-2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,55 @@ if (conditionToTest1) {
182182

183183
+ 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"`
184184

185+
**Top tip**: Remember, if you place a `return` statement before the end of the function, anything after `return` **won't get executed**. We can use this to make code terser:
186+
187+
```javascript
188+
function canGo(lightColor) {
189+
if (lightColor === 'green') {
190+
return true
191+
}
192+
193+
return false
194+
}
195+
```
196+
197+
The above function will return `true` if `lightColor` is `'green'` — go ahead
198+
and try it out.
199+
200+
```javascript
201+
canGo('green') // true
202+
```
203+
204+
And `false` otherwise:
205+
206+
```javascript
207+
canGo('red') // false
208+
```
209+
210+
Notice that we didn't have to use an `else` statement; we can just depend on
211+
`return`.
212+
213+
We need to be careful with `return`, however, because it's easy to return too
214+
early and not execute important parts of the function. For example,
215+
216+
```javascript
217+
function canGo(lightColor) {
218+
return true
219+
220+
if (lightColor === 'red') {
221+
return false
222+
}
223+
}
224+
```
225+
226+
will _always_ return `true`, even if `lightColor` is `'red'`. Try it!
227+
228+
```javascript
229+
canGo('red') // true
230+
```
231+
232+
And that's a great way to cause an accident.
233+
185234
### Ternary Operator
186235

187236
You can think of it as a shortcut for the `if-else` statement.
@@ -196,7 +245,6 @@ conditionToTest ? valueToBeReturnedIfTrue : valueToBeReturnedIfFalse
196245

197246
+ 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.
198247

199-
200248
## Switch Statements
201249

202250
Switch statements acts like a big if/else if/else chain. The switch expression is evaluated once and the value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed.
@@ -220,7 +268,7 @@ Example:
220268

221269
```javascript
222270
var mood = "hungry"
223-
switch(mood){
271+
switch(mood) {
224272
case "happy":
225273
console.log("Dance to Pharrel's Happy");
226274
break;
@@ -242,6 +290,26 @@ In the example above, we'll see `"You should eat a big chocolate cake"` printed
242290

243291
+ 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.
244292

293+
As with any function, `return` will halt execution at any point. Thus if we
294+
wrote,
295+
296+
```javascript
297+
function feelings(mood) {
298+
switch(mood) {
299+
case "happy":
300+
return "Dance to PHarrel's 'Happy'"
301+
default:
302+
return "I don't recognize that mood."
303+
}
304+
305+
console.log("Let us know how you're feeling tomorrow!")
306+
}
307+
```
308+
309+
the `console.log()` statement at the bottom of the function will
310+
_never run_. This is a major difference between `return` and `break`:
311+
`return` _exits_ the function and _returns_ a value; `break` exits a
312+
_block_ and does not (generally speaking) have a value associated with it.
245313

246314
## Resources
247315

bin/debug-ide

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
4+
5+
node_modules/.bin/node-inspector --hidden node_modules/ --hidden test/ --web-host $(/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}') --web-port $UID & node_modules/.bin/mocha --debug-brk --watch
6+
7+
trap - SIGINT SIGTERM EXIT

flow-control.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
'use strict';
2-
3-
function basicTeenager(age){
1+
function basicTeenager(age) {
42

53
}
64

7-
function teenager(age){
5+
function teenager(age) {
86

97
}
108

11-
function ageChecker(age){
9+
function ageChecker(age) {
1210

1311
}
1412

15-
function ternaryTeenager(age){
13+
function ternaryTeenager(age) {
1614

1715
}
1816

19-
function switchAge(age){
17+
function switchAge(age) {
2018

2119
}

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"test": "test"
88
},
99
"scripts": {
10+
"debug": "node-debug --hidden node_modules _mocha --watch",
11+
"debug-ide": "./bin/debug-ide",
1012
"test": "mocha -R mocha-multi --reporter-options nyan=-,json=.results.json"
1113
},
1214
"repository": {
@@ -26,10 +28,10 @@
2628
},
2729
"homepage": "https://github.com/learn-co-curriculum/skills-based-javascript-intro-to-flow-control#readme",
2830
"devDependencies": {
29-
"chai": "^3.5.0",
31+
"expect": "^1.20.2",
3032
"jsdom": "^8.5.0",
3133
"mocha": "^2.4.5",
32-
"mocha-jsdom": "^1.1.0",
33-
"mocha-multi": "^0.9.0"
34+
"mocha-multi": "^0.9.0",
35+
"node-inspector": "^0.12.8"
3436
}
3537
}

test/flow-control-test.js

+33-23
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,80 @@
1-
/*global describe, it */
2-
3-
const expect = require('chai').expect
1+
const expect = require('expect')
42
const fs = require('fs')
5-
const jsdom = require('mocha-jsdom')
3+
const jsdom = require('jsdom')
64
const path = require('path')
75

86
describe('flow-control', () => {
9-
jsdom({
10-
src: fs.readFileSync(path.resolve(__dirname, '..', 'flow-control.js'), 'utf-8')
7+
8+
before(done => {
9+
const src = path.resolve(__dirname, '..', 'flow-control.js')
10+
11+
jsdom.env('<div></div>', [src], (err, window) => {
12+
if (err) {
13+
return done(err)
14+
}
15+
16+
Object.keys(window).forEach(key => {
17+
global[key] = window[key]
18+
})
19+
20+
return done()
21+
})
1122
})
1223

1324
describe('basicTeenager', () => {
1425
it('should return "You are a teenager!" if the age is between 13-19', () => {
15-
expect(basicTeenager(13)).to.equal("You are a teenager!");
26+
expect(basicTeenager(13)).toEqual("You are a teenager!");
1627
})
1728

1829
it('should return undefined if the age is not between 13-19', () => {
19-
expect(basicTeenager(12)).to.be.undefined
30+
expect(basicTeenager(12)).toBe(undefined)
2031
})
2132

2233
})
2334

2435
describe('teenager', () => {
2536
it('should return "You are a teenager!" if the age is between 13-19', () => {
26-
expect(teenager(13)).to.equal("You are a teenager!")
37+
expect(teenager(13)).toEqual("You are a teenager!")
2738
})
39+
2840
it('should return "you are not a teenager" if the age is not between 13-19', () => {
29-
expect(teenager(12)).to.equal("You are not a teenager")
30-
expect(teenager(29)).to.equal("You are not a teenager")
41+
expect(teenager(12)).toEqual("You are not a teenager")
42+
expect(teenager(29)).toEqual("You are not a teenager")
3143
})
32-
3344
})
3445

3546
describe('ageChecker', () => {
3647
it('should return "You are a teenager!" if the age is between 13-19', () => {
37-
expect(ageChecker(13)).to.equal("You are a teenager!")
48+
expect(ageChecker(13)).toEqual("You are a teenager!")
3849
})
50+
3951
it('should return "You are a kid" if the age is 12 or below', () => {
40-
expect(ageChecker(12)).to.equal("You are a kid")
52+
expect(ageChecker(12)).toEqual("You are a kid")
4153
})
54+
4255
it('should return "You are a grownup" if the age is 20 or above', () => {
43-
expect(ageChecker(29)).to.equal("You are a grownup")
56+
expect(ageChecker(29)).toEqual("You are a grownup")
4457
})
45-
4658
})
4759

4860
describe('ternaryTeenager', () => {
4961
it('should return "You are a teenager" if age is between 13-19', () => {
50-
expect(ternaryTeenager(15)).to.equal("You are a teenager")
62+
expect(ternaryTeenager(15)).toEqual("You are a teenager")
5163
})
5264

5365
it('should return "You are not a teenager" if age not between 13-19', () => {
54-
expect(ternaryTeenager(75)).to.equal("You are not a teenager")
66+
expect(ternaryTeenager(75)).toEqual("You are not a teenager")
5567
})
56-
5768
})
5869

5970
describe('switchAge', () => {
6071
it('should return "You are a teenager" if age is between 13-19', () => {
61-
expect(switchAge(15)).to.equal("You are a teenager")
72+
expect(switchAge(15)).toEqual("You are a teenager")
6273
})
6374

6475
it('should return "You are not a teenager" if age not between 13-19', () => {
65-
expect(switchAge(75)).to.equal("You have an age")
66-
expect(switchAge(7)).to.equal("You have an age")
76+
expect(switchAge(75)).toEqual("You have an age")
77+
expect(switchAge(7)).toEqual("You have an age")
6778
})
68-
6979
})
7080
})

0 commit comments

Comments
 (0)