Can you describe this for me?
var arr, total;
arr = [1, 2, 3, 4, 5];
total = arr.reduce(function(previous, current) {
return previous + current;
});
// total
Array.reduceRight() is great when:
.
var bands = {
Beatles: [
{name: "John", instruments: "Guitar"},
{name: "Paul", instruments: "Guitar"},
{name: "George", instruments: "Guitar"},
{name: "Ringo", instruments: "Drums"}]
};
function listBandplayers(bandname, instrument) {
var bandmembers = bands[bandname];
var arr = [ "" , 0 , ` of ${bandmembers.length} ${bandname} play ` , instrument , "",
"\n" , ...bandmembers , "\n
" ];
var countidx = 1;
return arr.reduceRight((html, item, idx, _array) => {
if (typeof item === 'object') {
if (item.instruments.contains(instrument)) _array[countidx]++;
item = `\n\t- ` + item.name + "
";
}
return item + html;
});
}
console.log( listBandplayers('Beatles', 'Drums') );
/*
1 of 4 Beatles play Drums
- John
- Paul
- George
- Ringo
*/
console.log( listBandplayers('Beatles', 'Guitar') );
/*
3 of 4 Beatles play Guitar
- John
- Paul
- George
- Ringo
*/