0
var foo = 0;
var bar = 0;
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [6, 7, 8, 9, 10];

function getSum(arr, sum) {
    sum = arr.reduce(function(accum, val) {
      return accum += val;
    }, 0)
}

getSum(arr1, foo); //expect foo to equal 15, but it stays at 0
getSum(arr2, bar); //expect bar to equal 40, but it stays at 0

I'd like to use this function multiple times, hence the arguments. I know having the sum argument equal Array.reduce() isn't what you're supposed to do, but I'm really not sure what the syntax should be.

Basically I want arr1 values to always be added up for foo, and arr2 always added up for bar, without having to repeat my code.

  • 2
    You're not returning anything from `getSum`, it should be `undefined`, not 0. You also do not have `data` anywhere in your code. Fix that to `arr`, and it works as expected. – CertainPerformance Oct 06 '18 at 22:31
  • If you want to provide an initial argument to `reduce`, pass it as the second parameter (instead of using 0) – CertainPerformance Oct 06 '18 at 22:33
  • You can't pass a variable into a function like this and expect to assign a value back to that variable. You need to return the value from the function and assign that returned variable to the function: `foo = getSum(arr)` – Mark Oct 06 '18 at 22:33
  • the `sum` arg get passed by value, not by reference. this is because `sum` is a primitive type (`Number`). – Davin Tryon Oct 06 '18 at 22:35

2 Answers2

2

If I understand you correctly you just need a sum function like this:

var foo = 0;
var bar = 0;
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [6, 7, 8, 9, 10];

const getSum = (arr) => arr.reduce((r,c) => r+c)

foo = getSum(arr1)
bar = getSum(arr2)
 
console.log(foo)
console.log(bar)
Akrion
  • 18,117
  • 1
  • 34
  • 54
  • 1
    First part of your answer is perfect. Second part is absolute nonesense. `foo` and `bar` won't be assigned anything. They will always have `0` as value – ibrahim mahrir Oct 06 '18 at 22:46
  • 1
    Yeah wrong test there. Good point! – Akrion Oct 06 '18 at 22:50
  • One more tiny note: IMHO, It is preferable to use a regular function instead of an arrow one for consistency reasons (OP may not be familiar with arrow functions syntax, so he may not be able to see the improvement your answer provide over his code). – ibrahim mahrir Oct 06 '18 at 22:55
1

In JavaScript, primitive types are passed by value. Objects are passed by reference. You can accomplish this by wrapping foo up in an object, but you really should not design code this way. Code that mutates function arguments is often very hard to reason about (and debug).

var foo = { value: 0 };
var bar = { value: 0 };
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [6, 7, 8, 9, 10];
    
function getSum(arr, sum) {
    sum.value = arr.reduce(function(accum, val) {
      return accum += val;
    }, 0)
}
    
getSum(arr1, foo);
getSum(arr2, bar);

console.log(foo.value);
console.log(bar.value);

If possible, you should try to find a way to solve your problem without mutation.

In fact, many linting tools (like eslint) will throw an error if they see function arguments being mutated.

Instead, you should separate the sum function from the assignment of the resulting value:

var arr1 = [1, 2, 3, 4, 5];
var arr2 = [6, 7, 8, 9, 10];
        
function sum(arr) {
    return arr.reduce(function(acc, next) { return acc + next });
}

var foo = sum(arr1);
var bar = sum(arr2);
    
console.log(foo);
console.log(bar);
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
  • The second snippet, with `arr.reduce` being returned seems to work. I thought I always had to assign a value to `Array.reduce()`, but doing it where I just return the method is working. Thanks. –  Oct 06 '18 at 23:06
  • 1
    @00Saad if no initial value is supplied to `reduce` it uses the first value of the array as the initial value. – Davin Tryon Oct 07 '18 at 07:54