-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfOddNumbers.js
More file actions
40 lines (33 loc) · 876 Bytes
/
Copy pathSumOfOddNumbers.js
File metadata and controls
40 lines (33 loc) · 876 Bytes
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
//Given the triangle of consecutive odd numbers:
// 1
// 3 5
// 7 9 11
// 13 15 17 19
// 21 23 25 27 29
// ...
// Calculate the sum of the numbers in the nth row of this triangle (starting at index 1) e.g.: (Input --> Output)
// 1 --> 1
// 2 --> 3 + 5 = 8
function rowSumOddNumbers(n){
let result = 0;
let arr = []
let parameterSum = 0 ;
let numberStart = 1;
let counter = 0;
for(let i = 1 ; i <= n; i++){
parameterSum += i
}
while (counter < parameterSum){
if(numberStart % 2 == 1){
arr.push(numberStart)
counter ++
}
numberStart++
}
let lastArr = arr.slice(-n);
for(let i = 0 ; i < lastArr.length ; i++){
result += lastArr[i]
}
return result
}
console.log(rowSumOddNumbers(3))