wall.91
wall.91

Reputation: 33

Sum All Numbers in a Range

freecodecamp algorithm challenge. I can't determine at which point in my function I am returning more than one value. The correct return value should be 10. The console.log I print out in this function shows three values, with the one in the middle being the correct one. How do I target this specific value and ignore the other ones? https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range

function sumAll(arr) {
    for (let i = 0; i < arr.length; i++){
      if (arr[i] != arr[arr.length -1]){
        let num = arr[i] + 1;
        arr.splice(arr[i], 0, num);
          console.log(arr.reduce(function(sum, value) {
            return sum + value;
          }, 0));
      }
    }
}

sumAll([1, 4]);

Upvotes: 2

Views: 665

Answers (6)

Ivor Guzman
Ivor Guzman

Reputation: 1

function sumAll(arr) {
  arr.sort((a, b) => a - b);
  return arr[0] == arr[1] ? arr[0] : arr[0] + sumAll([arr[0] + 1, arr[1]])
}
console.log(sumAll([1, 4]));

function sumAll(arr) {
  arr.sort((a, b) => a - b);
  return arr[0] == arr[1] ? arr[0] : arr[0] + sumAll([arr[0] + 1, arr[1]])
}
console.log(sumAll([1, 4]));

Upvotes: 0

Paolo John
Paolo John

Reputation: 1

Here is another way:

 const sumAll = (arr) => {
    arr.sort((a, b) => a - b)
    return ((arr[0]+arr[1])/2)*((arr[1]-arr[0])+1)
 }

Upvotes: 0

Gautam_Mehta
Gautam_Mehta

Reputation: 1

You can use reacursion like this :

function sumAll(arr) {
  arr.sort((a, b) => a - b);
  return arr[0] == arr[1] ? arr[0] : arr[0] + sumAll([arr[0] + 1, arr[1]])
}
console.log(sumAll([1, 4]));

Upvotes: 0

Gautam_Mehta
Gautam_Mehta

Reputation: 1

You don't need to take the harder way. First you can use a simple for loop and sort and Second you can use recursion which i prefer because you just need one line of code to it. Here are my answers =>

/*function sumAll(arr) {
  arr.sort((a, b) => a - b);
  let sum = 0;
  for (let i = arr[0]; i <= arr[1]; i++) {
    sum += i;
  }
  return sum;
}

console.log(sumAll([1, 4]));*/


function sumAll(arr) {
  arr.sort((a, b) => a - b);
  return arr[0] == arr[1] ? arr[0] : arr[0] + sumAll([arr[0] + 1, arr[1]])
}
console.log(sumAll([1, 4]));

Upvotes: 0

brk
brk

Reputation: 50291

The main issue seems to be with this line arr.splice(arr[i], 0, num);. It is not advisable to change the length of the array which is currently under iteration.

function sumAll(arr) {
  let num = [];
  let x = arr[0];
  while (x <= arr[1]) {
    num.push(x);
    x++;
  }
  return num.reduce((acc, curr) => {
    return acc += curr

  }, 0)
}

console.log(sumAll([1, 4]));

Upvotes: 1

Zachiah
Zachiah

Reputation: 2627

If you extract a range utility function you can do this a lot easier. Without the imperitiveness

const range = (a,b) => Array(Math.abs(a-b)+1).fill(0).map((_,i) => i+Math.min(a,b))
const sumAll = ([a,b]) => range(a,b).reduce((c,d) => c+d,0)

to diagnose exactly what is wrong with your code, A good start is that it isn't returning anything, it is console.logging, so if you replace console.log with return.

Upvotes: 1

Related Questions