Answers for "sum([2, 3, 4], 1) should equal 2."

0

sum([2, 3, 4], 1) should equal 2.

function sum(arr, n) {

  // Only change code below this line

  if (n <= 0)// if n is less than or equals to 0{

    return 0 // should return zero

  } else {

    return sum(arr, n-1) + arr[n-1];

  }

  // Only change code above this line

}

sum([1],0);
Posted by: Guest on April-05-2021
0

sum([2, 3, 4], 1) should equal 2.

function sumAll(arr) {
  let max = Math.max(arr[0], arr[1]);
  let min = Math.min(arr[0], arr[1]);
  let sumBetween = 0;
  for (let i = min; i <= max; i++) {
    sumBetween += i;
  }
  return sumBetween;
}

sumAll([1, 4]);
Posted by: Guest on April-05-2021

Code answers related to "sum([2, 3, 4], 1) should equal 2."

Browse Popular Code Answers by Language