-3
function sumTwoSmallestNumbers(numbers) {  
  const sorted = numbers.sort((a, b) => {
    a - b;
  });
  return sorted[0] + sorted[1];
}

Its a question from code wars. This is my result but its failing one of the tests, where I sorted the array and tried to add the two smallest numbers. Can somone please help?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
zcode97
  • 13
  • 2

1 Answers1

1

Probaby you use array function in wrong way, in this algotithm you must return value a-b inside sort function

function sumTwoSmallestNumbers(numbers) {  
  const sorted = numbers.sort((a, b) => {
    return a - b;
  });
  return sorted[0] + sorted[1];
}

or

function sumTwoSmallestNumbers(numbers) {  
  const sorted = numbers.sort((a, b) =>  a - b);
  return sorted[0] + sorted[1];
}