array-map-1

/*
    Let' s create our array
*/

const n = [2, 4, 6, 8, 10];

/*
    map method requires callback function
    that will be ran agains every element
    in the provided array. 
    
    There are two ways to specify callback
    function. First, we can specify it inside
    map method itself, or we can create separate 
    function and provide this function to map
    method.
*/

//Provide function inside map method

/*
    We create variable r that will hold 
    returned array. Then we run map
    against const n. Our callback function
    is simple arrow function that multiplies 
    every item from const n by 2.
*/

const r = n.map(item => item * 2);

console.log(r);
//Output: [4, 8, 12, 16, 20]


//Create separate function and pass it to map

/*
    Second example does the same as first.
    Difference is that in second example we provide
    separate function and pass it to map.
    
    This is better way in terms that you can reuse
    function on multiple places
*/

const s = n.map(multiply);

function multiply(item) {
    return item * 2;
}

console.log(s);
//Output: [4, 8, 12, 16, 20]