Kristyn Bryan 10 years ago
commit 719549e305

@ -78,6 +78,8 @@ Expected result:
Add together all numbers in an array: Add together all numbers in an array:
``` ```
arr = [8, 8, 8, 8, 8, 8, 8, 8];
new_value = arr.reduce(function(sum, n){ new_value = arr.reduce(function(sum, n){
return sum += n return sum += n
}); });
@ -91,7 +93,7 @@ new_value = arr.reduce(function(sum, n){
arr = [8, 8, 8, 8, 8, 8, 8, 8]; arr = [8, 8, 8, 8, 8, 8, 8, 8];
``` ```
- Return the product of the numbers in `arr`. - Return the *product* of the numbers in `arr`.

@ -1,9 +1,5 @@
# W02D02 Morning Warmup # W02D02 Morning Warmup
![Cat Programmer](http://s2.quickmeme.com/img/99/9903c7c14add3fd0758b7b5b80c24d48101f296f13ce34736799a82c71f61bc2.jpg)
# JAVASCRIPT ITERATORS # JAVASCRIPT ITERATORS
#####`.forEach`, `.map`, and `.reduce` #####`.forEach`, `.map`, and `.reduce`
@ -27,7 +23,6 @@ arr.forEach(function(n) {
``` ```
##### EXERCISE: ##### EXERCISE:
- Log each word in `words` in upper case using `.forEach.` - Log each word in `words` in upper case using `.forEach.`
@ -35,8 +30,16 @@ arr.forEach(function(n) {
words = ["Ho", "Chi", "Minh", "City", "was", "once", "known", "as", "Prey", "Nokor"]; words = ["Ho", "Chi", "Minh", "City", "was", "once", "known", "as", "Prey", "Nokor"];
``` ```
<hr>
SOLUTION to .forEach
```javascript
words = ["Ho", "Chi", "Minh", "City", "was", "once", "known", "as", "Prey", "Nokor"];
words.forEach(function (e){
console.log(e.toUpperCase());
});
```
# .map # .map
@ -66,6 +69,17 @@ Expected result:
``` ```
["J", "O", "E"] ["J", "O", "E"]
``` ```
SOLUTION: .map
```javascript
more_words = ["Joe", "Overreats", "Eggs"];
var new_arr = more_words.map(function (i){
return i.charAt(0);
});
console.log (new_arr);
```
... ...
@ -93,7 +107,16 @@ arr = [8, 8, 8, 8, 8, 8, 8, 8];
- Return the product of the numbers in `arr`. - Return the product of the numbers in `arr`.
SOLUTION .reduce
```javascript
arr = [8, 8, 8, 8, 8, 8, 8, 8];
new_value = arr.reduce(function(product, n){
return product *= n
});
console.log(new_value);
```
... ...
@ -128,7 +151,7 @@ var sheepShearers = [
/////////////////////////// ///////////////////////////
SOLUTION: sheepShearer SOLUTION: sheepShearer
```javascript
var sheepShearers = [ var sheepShearers = [
{ {
name: "Tim", name: "Tim",
@ -157,7 +180,7 @@ var total = sheepShearers
}); });
console.log(total); console.log(total);
```
#### FINISHED EARLY?: #### FINISHED EARLY?:

Loading…
Cancel
Save