Skip to content

Commit 24b2ac3

Browse files
committed
clean up reduce
1 parent cf8119a commit 24b2ac3

File tree

1 file changed

+0
-74
lines changed

1 file changed

+0
-74
lines changed

reduce/core.js

-74
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,20 @@
1-
'use strict';
2-
var input = [ 10, 15, 20, 25, 30, 35 ];
3-
// var expected = 135
41
function sum (array) {
52
// your code here
6-
return array.reduce(function(initialValue, element){
7-
console.log(initialValue);
8-
console.log(element);
9-
return initialValue + element;
10-
});
113
}
124

13-
console.log(sum(input));
14-
15-
var input1 = [[ 1, 2, 3 ], //row
16-
[ 4, 5 ],
17-
[ 6 ]];
18-
19-
//output = 720
20-
215
function productAll (array) {
226
// your code here
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);
377
}
388

39-
console.log(productAll(input1));
40-
41-
42-
var cartoonsObject = [[ 'Thundercats', '80s' ], //row
43-
[ 'The Powerpuff Girls', '90s' ], //row
44-
[ 'Sealab 2021', '00s' ]]; //row
45-
// var expected = { 'Thundercats': '80s',
46-
// 'The Powerpuff Girls': '90s',
47-
// 'Sealab 2021': '00s' };
48-
499
function objectify (array) {
5010
// your code here
51-
//cartoonObject is our accumulator object, so also need {}
52-
//reach row is the element being passed in which is an array
53-
//console log to get the keys and values to populate our cartoonObject
54-
//create cartoonObject
55-
//return cartoonObject starting object/array/value
56-
return array.reduce(function(newCartoonObject, cartoonArray){
57-
// console.log('array ', array);
58-
// console.log(row);
59-
// console.log('row ', row[0]);
60-
// console.log('row2 ', row[1]);
61-
//build up cartoonObject object[key]
62-
newCartoonObject[cartoonArray[0]] = cartoonArray[1];
63-
// console.log('cartoonObject ', cartoonObject);
6411

65-
// console.log('cartoon Object ', cartoonObject);
66-
return newCartoonObject;
67-
},
68-
{}); //empty object container for newCartoonObject
6912
}
7013

71-
//to get cartoonObject without console logging before return
72-
console.log(objectify(cartoonsObject));
73-
74-
var input3 = [ 30, 48, 11, 5, 32 ];
75-
// var expected = 'Your lucky numbers are: 30, 48, 11, 5, and 32';
76-
77-
//define fortune string which is starting value 'Your lucky numbers are: '
7814
function luckyNumbers (array) {
7915
// your code here
80-
return array.reduce(function(previous, current, index, array) {
81-
// console.log('Your lucky numbers are: ' + element + element;
82-
if ( index === array.length - 1){
83-
console.log(previous + "and " + current);
84-
return previous + "and " + current;
85-
}
86-
return previous + current + ", ";
87-
console.log(previous + current);
88-
}, "Your lucky numbers are ");
8916
}
9017

91-
luckyNumbers(input3);
9218

9319
module.exports = {
9420
sum: sum,

0 commit comments

Comments
 (0)