-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
sum-arrays.js
91 lines (74 loc) · 1.62 KB
/
sum-arrays.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
function sum(numbers) {
let total = 0;
for (let i = 0; i < numbers.length; i++) {
total += numbers[i];
}
return total;
}
function sum(numbers) {
let taco = 0;
for (let i = numbers.length - 1; i >= 0; i--) {
taco += numbers[i];
}
return taco;
}
function sum(numbers) {
let total = 0;
for (let i = numbers.length - 1; i >= 0; i--) {
total += numbers[i];
}
return total;
}
function sum(numbers) {
let total = 0;
for (let i = 0; i < numbers.length; i++) {
const value = numbers[i];
total += value;
}
return total;
}
function sum(numbers) {
let total = 0;
const addTotal = (value, i, array) => {
total += value;
};
numbers.forEach(addTotal);
return total;
}
function sum(numbers) {
let total = 0;
numbers.forEach((value) => {
total += value;
});
return total;
}
function sum(numbers) {
return numbers.reduce((total, value, i, array) => {
total += value;
return total;
}, 0);
}
function sum(numbers) {
return numbers.reduce((total, value, i, array) => {
return total + value;
}, 0);
}
function sum(numbers) {
return numbers.reduce((total, value) => total + value, 0);
}
console.log(sum([]), 0);
console.log(sum([1, 5.2, 4, 0, -1]), 9.2);
const result = [1, 1, 6, 3, 8, 5, 5, 12, 9, 3, 7]
.reduce((counts, value) => {
counts[value] = counts[value] || 0;
// if (!counts[value]) {
// counts[value] = 0;
// }
counts[value]++;
return counts;
}, {});
console.log(result);
console.log(Object.keys(result));
// Dictionary<Int, Animal>
// const dictionary = {};
// dictionary[JSON.stringify({ name: 'CJ' })]