Permalink
Browse files

added final notes after delivering lesson

  • Loading branch information...
deconstructionalism
deconstructionalism committed Jun 13, 2018
1 parent 13daf7d commit 40ab2fdd3c9e0ab062ad8f6551fab1bc3cdbf625
@@ -212,4 +212,170 @@
students.map(text => `${text.toUpperCase()}!`)
```

#### Lab Code

Here is the code we used today while going over the Lab.

- **`bin/for-each.js`**

- ```js
#!/usr/bin/env node
'use strict'
// Example array
const nums = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
// 1. Find the sum of all elements in an array using named arrow function
// CODE ALONG CODE GOES HERE
// OLD SCHOOL WAY OF DOING THIS WITHOUT REDUCE
let accumulator = 0
for (let i = 0; i < nums.length; i++) {
accumulator = accumulator + nums[i]
}
console.log(accumulator)
// USING REDUCE INSTEAD
const reducer = (accumulator, currentVal) => accumulator + currentVal
const sum = nums.reduce(reducer)
console.log(sum)
// USING REDUCE WITHOUT DECLARING A SEPARATE NAMED FUNCTION
// notice that the names of the parameters for the reduce callback
// can be whatever you want to call them (compare this code to the
// previous section's code)
sum = nums.reduce((prev, curr) => prev + curr)
console.log(sum)
// 2. Find the largest of all elements in an array using named arrow function
// CODE ALONG CODE GOES HERE
const largest = nums.reduce((prev, curr) => prev > curr ? prev : curr)
console.log(largest)
// 3. Find the total number of days in fridge for all the food in the fridge
// using anonymous arrow function
// start the count with 0
const fridge = [
{ food: 'Carrots', daysInFridge: 32 },
{ food: 'Onions', daysInFridge: 23 },
{ food: 'Bell Peppers', daysInFridge: 16 },
{ food: 'Soda', daysInFridge: 7 },
{ food: 'Pizza', daysInFridge: 4 },
{ food: 'Cake', daysInFridge: 3 }
]
// CODE ALONG CODE GOES HERE
const howLazyYouAre = (prev, curr) => {
const numDays = curr.daysInFridge
return prev + numDays
}
fridge.reduce(howLazyYouAre, 0)
```

- **`bin/map.js`**

- ```js
#!/usr/bin/env node
'use strict'
// Starting array
const developers = ['Mike', 'Toni', 'Jessica']
// Iterate using FOR loop to create an array of 'Hello <name>' for each student
// Original array unchanged
let developerGreetings = []
for (let i = 0; i < developers.length; i++) {
developerGreetings.push('Hello ' + developers[i])
}
// Iterate using .forEach to create an array of 'Hello <name>' for each student
// Original array unchanged
// note: forEach returns undefined
developerGreetings = []
developers.forEach(developer => developerGreetings.push('Hello ' + developer))
// example of above code using standard JS function
developers.forEach(function(developer) {
developerGreetings.push('Hello ' + developer)
})
// 1. Iterate using .map to create an array of 'Hello <name>' for each student
// Original array unchanged
// CODE ALONG CODE GOES HERE
developerGreetings = developers.map(developer => `Hello ${developer}`)
```

- **`bin/reduce.js`**

- ```js
#!/usr/bin/env node
'use strict'
// Example array
const nums = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
// 1. Find the sum of all elements in an array using named arrow function
// CODE ALONG CODE GOES HERE
// OLD SCHOOL WAY OF DOING THIS WITHOUT REDUCE
let accumulator = 0
for (let i = 0; i < nums.length; i++) {
accumulator = accumulator + nums[i]
}
console.log(accumulator)
// USING REDUCE INSTEAD
const reducer = (accumulator, currentVal) => accumulator + currentVal
const sum = nums.reduce(reducer)
console.log(sum)
// USING REDUCE WITHOUT DECLARING A SEPARATE NAMED FUNCTION
// notice that the names of the parameters for the reduce callback
// can be whatever you want to call them (compare this code to the
// previous section's code)
sum = nums.reduce((prev, curr) => prev + curr)
console.log(sum)
// 2. Find the largest of all elements in an array using named arrow function
// CODE ALONG CODE GOES HERE
const largest = nums.reduce((prev, curr) => prev > curr ? prev : curr)
console.log(largest)
// 3. Find the total number of days in fridge for all the food in the fridge
// using anonymous arrow function
// start the count with 0
const fridge = [
{ food: 'Carrots', daysInFridge: 32 },
{ food: 'Onions', daysInFridge: 23 },
{ food: 'Bell Peppers', daysInFridge: 16 },
{ food: 'Soda', daysInFridge: 7 },
{ food: 'Pizza', daysInFridge: 4 },
{ food: 'Cake', daysInFridge: 3 }
]
// CODE ALONG CODE GOES HERE
const howLazyYouAre = (prev, curr) => {
const numDays = curr.daysInFridge
return prev + numDays
}
fridge.reduce(howLazyYouAre, 0)
```


Binary file not shown.

0 comments on commit 40ab2fd

Please sign in to comment.