DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Updated on

#31 - Sequences and Series CodeWars Kata (6 kyu)

Instructions

Have a look at the following numbers.

n score
1 50
2 150
3 300
4 500
5 750

Can you find a pattern in it? If so, then write a function getScore(n)/get_score(n)/GetScore(n) which returns the score for any positive number n.

Note Real test cases consists of 100 random cases where 1 <= n <= 10000


My solution:

function getScore(n) {
  let r = 0; 
  for(let i = 1; i<=n; i++){
    r += i*50
  }
  return r
}
Enter fullscreen mode Exit fullscreen mode

Explanation

First I had to identify the pattern, I saw that every score is equal to the sum of each number by 50, I did a for loop that iterated until "i" is equal to "n", and in each iteration I summed the "r" actual value plus the result of "i" by 50, that way I can get the last result.

let r = 0;
for(let i = 1; i<=n; i++){
r += i*50
}

at the end of the for loop I just returned "r"

return r


What do you think about this solution? 👇🤔

My Github
My twitter
Solve this Kata

Top comments (0)