Answers for "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."

0

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));
Posted by: Guest on March-21-2021
0

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) {
    let lower, higher;
    let result = 0;
    //return either of it if they are equal
    if(a == b) {
        return a;
    } else {
        if(a > b) {
            higher = a;
            lower = b;
        } else {
            higher = b;
            lower = a;
        }
        for(i = lower; i <= higher; i++) {
            result += i;
        }
    }
    return result;
}
console.log(GetSum(3, 9))
Posted by: Guest on March-21-2021
0

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.

return 2 + getSum(2 + 1, 3)
Posted by: Guest on March-21-2021
0

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;
}
Posted by: Guest on March-21-2021
0

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; //(1)
   if (a < b) {
      return a + getSum(a+1, b); //(2)
   }else {
      return a + getSum(a-1,b); //(3)
   }
}

getSum(1,3)
Posted by: Guest on March-21-2021
0

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.

getSum(1,3)
 1 + getSum(2,3)
 1 + ( 2 + getSum(3,3))
 1 + ( 2 + (3))
Posted by: Guest on March-21-2021
0

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) => 1/2 * Math.abs((a ** 2 + a - b ** 2 - b));
Posted by: Guest on March-21-2021
0

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.

return 1 +  getSum(1 + 1, 3)
Posted by: Guest on March-21-2021
0

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) => a === b ? a : getSum(a + (a < b)?1:-1, b);
Posted by: Guest on March-21-2021

Code answers related to "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."

Browse Popular Code Answers by Language