javascript array reduce method

by Ken Sprague

JavaScript

//array reduce method
//reduce all the values in an array into a single result
//array.reduce(callback, initialValue)
//also has a second parameter which is an initialValue



let numbers = [19, 28, 65, 72, 93];
//find the sum of all the numbers


let movies = ['Batman Dark Knight', 'Deadpool 2', 'Avengers', 'Iron Man', 'Spiderman', 'Superman', 'Swamp Thing', 'Ghost Rider', 'Daredevil', 'Ghost in a Shell', 'Akira', 'Gaurdians of the Galaxy', 'Aquaman', 'Wonder Woman', 'Deapool', 'Xmen'];
//find the first movie aphabetically

let sum = numbers.reduce(function(passedIn, item){
		//show the item being passed in
    console.log(passedIn, item);
		return passedIn + item;
},0);
console.log('Total is', sum);


let first = movies.reduce(function(current, item){
		console.log('comparing', current, 'to', item);
		return (current < item) ? current: item;
}, "\u4034");
console.log('First movie is', first);