본문 바로가기
알고리즘/Codewars

[Codewars] Sum of a sequence (7 kyu) / JavaScript

by fluss 2023. 2. 27.

https://www.codewars.com/kata/586f6741c66d18c22800010a

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Your task is to make function, which returns the sum of a sequence of integers.

The sequence is defined by 3 non-negative values: begin, end, step (inclusive).

If begin value is greater than the end, function should returns 0

 

Examples

2,2,2 --> 2
2,6,2 --> 12 (2 + 4 + 6)
1,5,1 --> 15 (1 + 2 + 3 + 4 + 5)
1,5,3  --> 5 (1 + 4)

This is the first kata in the series:

  1. Sum of a sequence (this kata)
  2. Sum of a Sequence [Hard-Core Version]

 

코드

const sequenceSum = (begin, end, step) => {
  let result = 0;
  for(let i = begin; i <= end; i += step){
    result += i;
  }
  return result;
};

댓글