Home > Mobile >  Reduce of empty array with no initial value, how to sort function with two parameters from highest t
Reduce of empty array with no initial value, how to sort function with two parameters from highest t

Time:07-18

I am running npm test for my code and I am failing the third test out of six test. I've tried to sort it with the following :

sumAll.sort(function(min,max)) {
    return max - min;
}
    

But it's not working. I tried to add conditionals in the code by using 'if (min > max )... else if ( min < max )' but it still don't work too. I tried adding '0' on the reducer variable 'accumulator currentValue, 0' but still it's not working. Is there any way how can I sort the sumAll function so it's still working even if it use a higher 'min' arguments than the 'max' argument ? Please help.

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');
  });
});

CodePudding user response:

The reducer to sum array value is :

arr.reduce((ac, cv) => ac   cv, 0);

Add a initial value should prevent error : empty array with no initial value

This code works for me :

const sumAll = function( min, max ) {
    let fullArr = [];
    let sum = 0; 

    for ( let i = min; i <= max; i  ) {
        fullArr.push(i);
    }

    sum = fullArr.reduce((ac, cv) => ac   cv, 0);

    return sum;
}

console.log(sumAll(1,4));
console.log(sumAll(123,1));
// Output 10
// Output 0 (because min < max)

If you want sumAll(123, 1) to print 7626 you have to switch both min and max when min > max

For exemple use this for loop :

    for ( let i = (min <= max ? min : max); i <= (max >= min ? max : min); i  ) { }

or as suggested by @adiga :

if( min > max) [min, max] = [max, min];
  • Related