My npm test is not passing the third out of six tests. I have attempted to sort it using the following code snippet:
sumAll.sort(function(min,max)) {
return max - min;
}
However, this approach did not work. I also tried incorporating conditionals in the code using 'if (min > max )... else if ( min < max )', but that didn't work either. I even attempted adding '0' to the reducer variable 'accumulator + currentValue, 0', but I'm still facing issues. Is there a way to sort the sumAll function so that it works properly even when the 'min' argument is higher than the 'max' argument? Any help would be appreciated.
const sumAll = function( min, max ) {
let fullArr = [];
let sum = 0;
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// let highestToLowest =
for ( let i = min; i <= max; i++) {
fullArr.push(i);
}
// sumAll.sort(function(min,max)) {
// return max - min;
// }
// // let lowestToHighest = fullArr.sort((a, b) => a - b);
// let highestToLowest = fullArr.sort((min, max) => max-min);
sum = fullArr.reduce(reducer);
return sum;
}
sumAll(1,4);
sumAll(123, 1); <---------- I failed on this function call saying it 'Reduce
of empty array with no initial value....
---------------------------- Jest code --------------------------
const sumAll = require('./sumAll')
describe('sumAll', () => {
test('sums numbers within the range', () => {
expect(sumAll(1, 4)).toEqual(10);
});
test('works with large numbers', () => {
expect(sumAll(1, 4000)).toEqual(8002000);
});
test('works with larger number first', () => {
expect(sumAll(123, 1)).toEqual(7626);
});
test.skip('returns ERROR with negative numbers', () => {
expect(sumAll(-10, 4)).toEqual('ERROR');
});
test.skip('returns ERROR with non-number parameters', () => {
expect(sumAll(10, "90")).toEqual('ERROR');
});
test.skip('returns ERROR with non-number parameters', () => {
expect(sumAll(10, [90, 1])).toEqual('ERROR');
});
});