Daily Coding Challenge - Sum of Differences

Tell us what’s happening:

Unless I am completely misunderstanding this simple premise… all of the tests answers, except for the first test, are wrong. I can’t even begin to understand how they got what they are calling the correct answers. Can someone please break this down Barney style for me, so I can try and complete this challenge. Thank you!

Your code so far

function sumOfDifferences(arr) {

  let diff;
  let total = 0;

  for (let i = 0; i < arr.length; i++) {
    if (i === arr.length - 1) {
      continue;
    }
    if (arr[i] > arr[i + 1]) {
      diff = arr[i] - arr[i + 1];
    } else {
      diff = arr[i + 1] - arr[i];
    }
    total += diff;
  }

  return total;

}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36

Challenge Information:

Daily Coding Challenge - Sum of Differences

https://www.freecodecamp.org/learn/daily-coding-challenge/2026-05-26

GitHub Link: https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/blocks/daily-coding-challenge/69f8c998d78ad3171a0713bc.md

Hi @suphxx

The difference between -3 and 5 is -8, not 8.

Happy coding

You misunderstood problem.
I will explain you about second sample.

[5, -3, 3, 9, 10]
(-3 - 5) + (3 - (-3)) + (9 - 3) + (10 - 9)
= -8 + 6 + 6 + 1
= 5
But you are calculating sum of the abs of the differences between each number.
Please calculate sum of arr[i + 1] - arr[i].