Skip to content

Commit 77b34f1

Browse files
authored
Create Codewars_Gradually_Adding_Parameters.js
1 parent 9459012 commit 77b34f1

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Gradually Adding Parameters
3+
4+
This kata is all about adding numbers.
5+
6+
You will create a function named add. This function will return the sum of all the arguments. Sounds easy, doesn't it??
7+
8+
Well here's the twist. The inputs will gradually increase with their index as parameter to the function.
9+
10+
add(3,4,5);
11+
/*
12+
returns ( 3 * 1 ) + ( 4 * 2 ) + ( 5 * 3 ) = 26
13+
*/
14+
Remember the function will return 0 if no arguments are passed.
15+
16+
Example
17+
18+
add(); //=> 0
19+
add(1,2,3); //=> 14
20+
add(1,4,-5,5); //=> 14
21+
*/
22+
23+
24+
function add() {
25+
for (var i = 0, sum = 0; i < arguments.length; i++) {
26+
sum += (arguments[i] * (i+1));
27+
}
28+
return sum;
29+
}
30+
31+
32+
/*
33+
Time: 313ms Passed: 5 Failed: 0
34+
Test Results:
35+
Test Passed: Value == 0
36+
Test Passed: Value == 1400
37+
Test Passed: Value == 2
38+
Test Passed: Value == -8
39+
Test Passed: Value == -30
40+
You have passed all of the tests! :)
41+
*/

0 commit comments

Comments
 (0)