Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of Parts of An Array - JavaScript

Trying to solve this challenge on codewars. According to the challenge, the parts of array:

ls = [0, 1, 3, 6, 10]

Are

ls = [0, 1, 3, 6, 10]
ls = [1, 3, 6, 10]
ls = [3, 6, 10]
ls = [6, 10]
ls = [10]
ls = []

And we need to return an array with the sums of those parts.

So my code is as follows:

function partsSums(ls) {
  let arrayOfSums = []; 
  while(ls.length > 0) {
    let sum = ls.reduce((a, b) => a + b);
    arrayOfSums.push(sum);
    ls.shift();
  }
return arrayOfSums;
}

console.log(partsSums([0, 1, 3, 6, 10]));

The issue is that it wants us to add the last sum 0 when the array is empty. So we should be getting:

[ 20, 20, 19, 16, 10, 0 ]

Instead of

[ 20, 20, 19, 16, 10]

So I tried this:

function partsSums(ls) {
  let arrayOfSums = []; 
  while(ls.length > 0) {
    let sum = ls.reduce((a, b) => a + b);
    arrayOfSums.push(sum);
    ls.shift();
  }
arrayOfSums.push(0);
return arrayOfSums;
}
console.log(partsSums([0, 1, 3, 6, 10]));

And this:

function partsSums(ls) {
  ls.push(0);
  let arrayOfSums = []; 
  while(ls.length > 0) {
    let sum = ls.reduce((a, b) => a + b);
    arrayOfSums.push(sum);
    ls.shift();
  }
return arrayOfSums;
}

But these caused execution time-out errors on Codewars:

Execution Timed Out (12000 ms)

So I also tried:

function partsSums(ls) {
  let arrayOfSums = []; 
  while(ls.length > -1) {
    let sum = ls.reduce((a, b) => a + b);
    arrayOfSums.push(sum);
    ls.shift();
  }
return arrayOfSums;
}

But now this causes a TypeError:

TypeError: Reduce of empty array with no initial value

I am not understanding the concept of how to get 0 into the array when all of the values have been shifted out. The challenge seems to want 0 as the final "sum" of the array, even when the array is empty. But you cannot reduce an empty array - what else can I do here?

EDIT: Tried adding initial value to the reduce method:

function partsSums(ls) {
  let arrayOfSums = []; 
  while(ls.length > 0) {
    let sum = ls.reduce((a, b) => a + b, 0);
    arrayOfSums.push(sum);
    ls.shift();
  }
return arrayOfSums;
}

Unfortunately this still fails the basic test :

expected [] to deeply equal [ 0 ]

like image 464
HappyHands31 Avatar asked Jun 24 '19 14:06

HappyHands31


People also ask

What is sum += in JavaScript?

The addition assignment operator ( += ) adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator.


1 Answers

Another solution that passed all of the tests:

function partsSums(ls) {
    let result = [0],
      l = ls.length - 1;
      
    for (let i = l; i >= 0; i--) {
        result.push(ls[i] + result[ l - i]);
    }
    return result.reverse();
}


console.log(partsSums([]));
console.log(partsSums([0, 1, 3, 6, 10])); 
console.log(partsSums([1, 2, 3, 4, 5, 6]));
console.log(partsSums([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]));
like image 92
Fraction Avatar answered Oct 15 '22 21:10

Fraction