|
1 | | -'use strict'; |
2 | | -var input = [ 10, 15, 20, 25, 30, 35 ]; |
3 | | -// var expected = 135 |
4 | 1 | function sum (array) { |
5 | 2 | // your code here |
6 | | - return array.reduce(function(initialValue, element){ |
7 | | - console.log(initialValue); |
8 | | - console.log(element); |
9 | | - return initialValue + element; |
10 | | - }); |
11 | 3 | } |
12 | 4 |
|
13 | | -console.log(sum(input)); |
14 | | - |
15 | | -var input1 = [[ 1, 2, 3 ], //row |
16 | | - [ 4, 5 ], |
17 | | - [ 6 ]]; |
18 | | - |
19 | | - //output = 720 |
20 | | - |
21 | 5 | function productAll (array) { |
22 | 6 | // 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); |
37 | 7 | } |
38 | 8 |
|
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 | | - |
49 | 9 | function objectify (array) { |
50 | 10 | // 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); |
64 | 11 |
|
65 | | - // console.log('cartoon Object ', cartoonObject); |
66 | | - return newCartoonObject; |
67 | | - }, |
68 | | - {}); //empty object container for newCartoonObject |
69 | 12 | } |
70 | 13 |
|
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: ' |
78 | 14 | function luckyNumbers (array) { |
79 | 15 | // 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 "); |
89 | 16 | } |
90 | 17 |
|
91 | | -luckyNumbers(input3); |
92 | 18 |
|
93 | 19 | module.exports = { |
94 | 20 | sum: sum, |
|
0 commit comments