Skip to content

Commit 743e6b0

Browse files
author
KatieAJenkins
committed
multiply working
1 parent 2bff346 commit 743e6b0

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

reduce/core.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,55 @@ function sum (array) {
1212

1313
console.log(sum(input));
1414

15+
var input1 = [[ 1, 2, 3 ], //row
16+
[ 4, 5 ],
17+
[ 6 ]];
18+
19+
//output = 720
20+
1521
function productAll (array) {
1622
// your code here
17-
};
23+
return array.reduce(function(product, row){
24+
console.log('product ', product);
25+
console.log('row ', row);
26+
//product starts at 1. will hold all the results of reduce on each row
27+
//
28+
product *= row.reduce(function(prev, current){
29+
console.log('prev ', prev);
30+
console.log('current ', current);
31+
console.log(prev * current);
32+
return prev * current;
33+
});
34+
console.log(product);
35+
return product;
36+
}, 1);
37+
}
38+
39+
console.log(productAll(input1));
40+
41+
42+
var input2 = [[ 'Thundercats', '80s' ],
43+
[ 'The Powerpuff Girls', '90s' ],
44+
[ 'Sealab 2021', '00s' ]];
45+
// var expected = { 'Thundercats': '80s',
46+
// 'The Powerpuff Girls': '90s',
47+
// 'Sealab 2021': '00s' };
1848

1949
function objectify (array) {
2050
// your code here
21-
};
51+
//cartoonObject is our accumulator object, so also need {}
52+
//reach row is the element being passed in which is an array
53+
return array.reduce(function(cartoonObject, row){
54+
55+
console.log('row ', row[0]);
56+
// console.log(newObject);
57+
58+
console.log(cartoonObject);
59+
return cartoonObject;
60+
}, {});
61+
}
62+
63+
objectify(input2);
2264

2365
function luckyNumbers (array) {
2466
// your code here

0 commit comments

Comments
 (0)