-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquareSum.js
53 lines (33 loc) · 892 Bytes
/
squareSum.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
// Square(n) Sum
// Complete the square sum function so that it squares each number passed into it and then sums the results together.
//For example, for [1, 2, 2] it should return 9 because 1^2 + 2^2 + 2^2 = 9.
//my Solution
function squareSum(number) {
// squares each number
// sums results
let sum = 0;
for (let i = 0; i < number.length; i++) {
sum += number[i] ** 2
}
return sum
}
squareSum(2, 2, 2)
//other interesting solutions
// 1
function squareSum(numbers){
return numbers.reduce(function(sum, n){
return (n*n) + sum;
}, 0)
}
// 2
function squareSum(numbers){
return numbers.reduce((sum,num) => sum + (num * num), 0);
}
// MDN explaination
const array1 = [1, 2, 3, 4];
// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
(previousValue, currentValue) => previousValue + currentValue,
initialValue
);