2

I'm studying Javascript using Marijn Haverbeke's book Eloquent JavaScript and didn't understand the following example:

function reduce(combine, base, array) {
    forEach(array, function (element) {
        base = combine(base, element);
    });
    return base;
}

function add(a, b) {
    return a + b;
}

function sum(numbers) {
    return reduce(add, 0, numbers);
}

The forEach function is one he introduces earlier in the book, which is the following:

function forEach(array, action) {
    for(var i = 0; i < array.length; i++)
        action(array[i]);
}

Now, back to the reduce function, what I don't understand is why, in the sum function, 0 is passed as base to reduce. Isn't that weird? Let's say I try to run sum([1,2,3])... wouldn't it look something like 0 = add(0,1) in its first loop? I don't get it.

1
  • 1
    It's not 0 = add(0, 1), it's just that the variable (i.e. base) is being assigned to a new value. It's not weird at all, because you're allowed to do so in JavaScript. It's not like this is Erlang or something. Commented Oct 1, 2012 at 12:34

3 Answers 3

2

After adding it is putting the sum to base only. So it is getting incremented on every loop.

base = combine(base, element)

This statement first computes combine(base, element) and assigns it to base. Its not comparing 0 and add(0, 1). So for the next iteration, base would have the sum for all the previous values.

EDITED

Suppose, you call reduce(combine, 0, [10, 22, 7, 5]). Loop will be iterated as

Iteration           base          element
1                   0             10

After computing add(base, element), base is set to 10. So for the next iteration, its value is 10.

1                   10            22

After computing add(base, element), base is set to 32. So for the next iteration, its value is 32.

1                   32            7

After computing add(base, element), base is set to 39. So for the next iteration, its value is 39.

1                   39            5

After computing add(base, element), base is set to 44. Since there are no more elements left, the answer is 44.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm still confused... When the function add is called, the value 0 is assigned to base. From this point on, shouldn't base be substituted for 0 whenever it's invoked inside the function? base is pointing to an address in the memory, right? How does the JavaScript engine know when it should use the value stored in the memory and when it should just "point" to it? In other words, why does the first base not get substituted by 0?
base is not an address, its just a value. It is getting substituted by 0 for the first iteration but later it is incremented by the value of each element Edited my answer for detailed explanation.
1

base is the variable that will contain the sum of all elements. It's normal that it starts with 0.

It's not : 0 = add(0,1)

but rather : base will contain the sum of previous value of base and 1

Comments

1

Remember as well that the content on the right side of the equals sign is evaluated first. So,

base = combine(base, element)

is taking the result of combine(base, element) and assigning it to the variable base (which now overwrites what base was previously assigned to).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.