functions and callbacks - JS : <p>I was given three functions that sends 2 parameter with callbacks, however I don't understand the codes even though there are comments on it and how can I log or manipulate data or the array or object using these functions.</p>

<pre><code>// Define a function, each, which takes in a collection and 
// uses a callback on the elements of collection
// Collection can be either an array or an object
var each = function(collection, callback){
    if(Array.isArray(collection)){
        for(var i=0;i&lt;collection.length;i++){
            callback(collection[i]);
        }
    }
    else{
        for(var key in collection){
            callback(collection[key]);
        }
    }
};


// Define a function, map, which uses a callback on a collection and returns an array
// example, times3 of the array, [3,6,9,12,15]
// Create an empty array and push the values from the callback 
// onto that array.  Then return the array

// You must use each function instead of a for-loop.
// Each element of collection will be passed as an argument into 
// the function parameter which then uses the call-back from the outer scope!

var map = function(collection, callback){
    var new_Array = [];
    each(collection, function(x){
           new_Array.push(callback(x));
    });
    return new_Array;
};


// Define a function filter, which checks the boolean value of the callback 
// on the element of collection
// output each boolean value of the callback
// example, even of an array, [1,2,3,4,5] -&gt; false, true, false, true, false
var filter = function(collection, callback){
    each(collection, function(item){
        if(callback(item)){
            return item;
        }
   });
};

var myObj = {
  num1: 1,
  num2: 2,
  num3: [3, 5, 6]
};

var myArr = [2, 4, 6, 8, 9];

var output = each(myObj);
console.log(output);
</code></pre>

<p>Any idea? Can anyone explain (in layman's term) to me how these code works and how to manipulate them?</p>
