Whats wrong with my code anybody please help me in this

(function(){
    var buffer = {
        entries:function(s) {
            this.entries.push(s);
        },
        concat:function() {
            return this.entries.join("");
        }
    };
    var source = ["123","456","789"];
    source.forEach(buffer.add,buffer);
    console.log(buffer.join());
})();

Don’t you know? What is it not doing, that you expect it to do?

I don’t think there is enough code here to help you. Please include everything.

There are a couple problems I can spot with the code:

  1. You define two methods on your buffer object, entries and concat, but you don’t call either of them. Your calling code calls buffer.add and buffer.join.
  2. Your entries method is trying to push to a non-existent array.

I’m guessing that what you actually wanted to do is something like this:

var buffer = {
    entries: [],
    add:function(s) {
        this.entries.push(s);
    },
    concat:function() {
        return this.entries.join("");
    }
};

var source = ["123","456","789"];

source.forEach(buffer.add, buffer);
console.log(buffer.concat()); // Outputs '123456789'
2 Likes