0

I am trying to understand the raw codes of the reduce function and how to use it.

    var each = function(collection, callback){
        if(Array.isArray(collection)){
            for(var i=0;i<collection.length;i++){
                callback(collection[i]);
            }
        }
        else{
            for(var key in collection){
                callback(collection[key]);
            }
        }
    };

var reduce = function(collection, callback, accumulator){
  each(collection, function(element){
    if(accumulator === undefined) {
      return accumulator = element;
    }else {
      return accumulator = callback(accumulator, element);
    };
});

return accumulator;
};

var sArr = [3, 2, 3, 4, 5];


each(sArr, function(collection){
  console.log(collection);
});

reduce(sArr, function(collection, prev){
  console.log(prev = prev + sArr);
});

On my codes above as you can see I was trying to reduce all of my arrays into one element by adding them all up just like a normal reduce function does but it doesn't work the way I think it is.

Can somebody check if I am doing wrong and if you can explain to me in layman's term what does my reduce function does line by line?

Sorry newbie.

8
  • 1
    What are you expecting and what are you getting? (I'm guessing you probably want your reduce function to actually return a value at the end) Commented Aug 8, 2017 at 1:06
  • developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Aug 8, 2017 at 1:06
  • @JosiahKeller: I am getting a series of nubmersinstead of reduce one number: "21,2,3,4,5" Commented Aug 8, 2017 at 1:08
  • If you're getting that, then whatever you're running is different from what you gave us, since your code can only produce ReferenceError: myArr is not defined. Make sure what you're testing is actually what you posted. :) Commented Aug 8, 2017 at 1:13
  • Edited. sorry my bad. Commented Aug 8, 2017 at 1:16

1 Answer 1

2
reduce(sArr, function(collection, prev){
  return collection + prev;
});

or equivalently

reduce(sArr, (collection, prev) => collection + prev);

will return the correct value. What you have:

reduce(sArr, function(collection, prev){
  console.log(prev = prev + myArr);
});

will try to add non-existent value (myArr) to the current element, then try to assign it back to the local variable (prev) that will be discarded at the end of the function, then display it, and finally return undefined to the accumulator because you don't have a return function.

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

5 Comments

tried this: reduce(sArr, function(collection, prev){ return collection + prev; }); but it doesnt give me anything on the console log.
There is no console.log in reduce(sArr, function(collection, prev){ return collection + prev; });. Did you try console.log(reduce(sArr, function(collection, prev){ return collection + prev; }));? Because that prints 17.
I think it works now. Thanks for that. Btw, I am a new beginner to this one. Mind trying to explain what my reduce function does line by line please? I am a dummy.
I don't know how to explain it without literally reading the code out loud line by line: it will iterate on collection, taking each element in turn; if accumulator is undefined (hopefully the first element when accumulator parameter wasn't passed) it will set it to the current element, otherwise it will set it to the result of applying callback to accumulator and element. There is a bug if callback returns undefined (accumulator gets set to the next element, effectively resetting the procedure mid-collection) that your code triggers, which JS reduce wouldn't do.
do you think i need to explicitly declare return undefined?

Your Answer

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