Given: Two positive integers a and b (a<b<10000). Return: The sum of all odd integers from a through b, inclusively. code example

Example 1: Given two integers a and b, which can be positive or negative, find the sum of all the integers between including them too and return it. If the two numbers are equal return a or b.

function GetSum(a, b) {
    if(a == b) {
        return a
    }
    else if (a < b) {
        return a + GetSum(a+1, b)
    } else {
        return a + GetSum(a-1, b)
    };
}
console.log(GetSum(-1, 2));

Example 2: Given two integers a and b, which can be positive or negative, find the sum of all the integers between including them too and return it. If the two numbers are equal return a or b.

const getSum = (a, b) => {
   const res = 0;
   if(b < a) [a,b] = [b,a];
   while(a <= b) res += a++;
   return res;
}

Tags:

Misc Example