-
Notifications
You must be signed in to change notification settings - Fork 0
/
7kyu-basicSequencePractice.js
41 lines (33 loc) · 1.41 KB
/
7kyu-basicSequencePractice.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
/*
A sequence or a series, in mathematics, is a string of objects, like numbers, that follow a particular pattern. The individual elements in a sequence are called terms. A simple example is 3, 6, 9, 12, 15, 18, 21, ..., where the pattern is: "add 3 to the previous term".
In this kata, we will be using a more complicated sequence: 0, 1, 3, 6, 10, 15, 21, 28, ... This sequence is generated with the pattern: "the nth term is the sum of numbers from 0 to n, inclusive".
[ 0, 1, 3, 6, ...]
0 0+1 0+1+2 0+1+2+3
Your Task
Complete the function that takes an integer n and returns a list/array of length abs(n) + 1 of the arithmetic series explained above. Whenn < 0 return the sequence with negative terms.
Examples
5 --> [0, 1, 3, 6, 10, 15]
-5 --> [0, -1, -3, -6, -10, -15]
7 --> [0, 1, 3, 6, 10, 15, 21, 28]
*/
//P: one input, an integer can be pos or neg
//R: return an array of integers where each element is the sum of the integers so far, up to n (inclusive)
//E: 3 => [0, 1, 3, 6]
//P: use a for loop to add the numbers to an array for n iterations
function sumOfN(n) {
let result = [0,];
let current = 0;
let incrementer = 0;
if(n > 0){
for(let i=1; i<=n; i++){
current += i;
result.push(current);
}
}else if(n < 0){
for(let i = -1; i>=n; i--){
current += i;
result.push(current);
}
}
return result
};