Array Reduce Method

Video

JavaScript Notes

JavaScript
    // reduce all the values in an array into a single result
    // uses a callback function just like map, forEach, filter, etc
    // array.reduce(callback, initialValue)
    // also has a second parameter which is an initial value
    
    let numbers = [12, 34, 56, 78, 91];
    // find the sum of all the numbers
     
    let movies = ['Star Wars', 'Star Trek', 'Jaws', 'Jurassic Park', 'Gross Pointe Blank', 'Eternal Sunshine of the Spotless Mind', 'Memento', 'Dog Soldiers', 'The Host', 'Gran Torino', 'Close Encounters of the Third Kind', 'Good Will Hunting', 'Layer Cake', 'Casino Royale', 'Almost Famous'];
    // find the first movie alphabetically
    
    let sum = numbers.reduce(function(accumulator, item ){ 
        console.log(accumulator, item);
        return accumulator + item;
    }, 0)
    console.log( 'Total is', sum '\n');
    
    let first = movies.reduce(function(current, item){ 
        console.log('comparing', current, 'to', item); return (current < item) ? current: item;
    }, "\u0434");
    console.log( 'First movie is', first);