0

I want to be able to pass an array name to a function that sums all the values of an array. However, I can't get this working with bracket notation the way that I would with an object.

UPDATE: Based on the comments below, I now realize that I can simply pass the array instead of passing it's name as a string. Here is a working example:

function sum(array) {
    const reducer = (accumulator, currentValue) => accumulator + currentValue;
    var x = array.reduce(reducer, 0);
    return x;
}

const arrayA = [1, 2, 3, 4];
const arrayB = [1, 2];

var x = sum(arrayA);
var y = sum(arrayB);

console.log('= ' + x);
console.log('= ' + y);

Below are the examples from my original question.

Here is the not working code that shows the syntax I'd like to use:

function sum(array) {
    const reducer = (accumulator, currentValue) => accumulator + currentValue;
    var x = [array].reduce(reducer, 0); // bracket notation, but doesn't seem to work
    return x;
}

const array = [1, 2, 3, 4];

var x = sum('array'); // should = 10

console.log('= ' + x);

Here is a working version of a reduce function that isn't passing the array name as a parameter (thus, doesn't suit my needs).

function sum() {
    const reducer = (accumulator, currentValue) => accumulator + currentValue;
    var x = array.reduce(reducer, 0);
    return x;
}

const array = [1, 2, 3, 4];

var x = sum();

console.log('= ' + x);
forestkelley
  • 341
  • 3
  • 14
  • this is a really weird way to do things... You might be able to get away with this[array].reduce but it depends on your context – bryan60 Apr 12 '19 at 15:50
  • I want to have a single function that can be called whenever I want to sum the values of an array. I assume this is a common need, instead of writing a unique `reduce` statement for every instance you need to sum the values of an array. – forestkelley Apr 12 '19 at 15:54
  • why `array` in `sum('array')` is a string? Why an you pass it as argument like `sum(array)` – brk Apr 12 '19 at 15:55
  • @forestkelley right, but if you can pass a string you could literally just pass in the array. – zfrisch Apr 12 '19 at 15:55
  • As others have said, you just pass the array instead of the string name of it... – bryan60 Apr 12 '19 at 15:57
  • @forestkelley also, the reason why I voted to reopen, is that your code is fundamentally incorrect. `[array]` is not going to grab the global `array` variable and there's no reason for it to. It literally translates to `["array"]` which is just a string element inside an Array. When you try to reduce it starts with `0` and then converts it to a string through concatenating with a `string`. Thus becomes `"0array"` – zfrisch Apr 12 '19 at 15:59
  • Well, this is all interesting. I'm not sure how I can dynamically pass more than one array to this function, even if simply passing the array instead of passing the array name as a string. But I'll look into your suggestions. Much appreciated. – forestkelley Apr 12 '19 at 16:03
  • Thanks @brk Simply passing the array was a good solution that I didn't think of and now understand. I added a working version of the code to my question. Thanks! – forestkelley Apr 12 '19 at 16:17

0 Answers0