This is code from an episode of Fun Fun Function: https://youtu.be/6sQDTgOqh-I
Last active
May 29, 2020 02:52
-
-
Save mpj/8256bdfae3168733a0bf733c604142d0 to your computer and use it in GitHub Desktop.
Arrow functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dragonEvents = [ | |
{ type: 'attack', value: 12, target: 'player-dorkman' }, | |
{ type: 'yawn', value: 40 }, | |
{ type: 'eat', target: 'horse' }, | |
{ type: 'attack', value: 23, target: 'player-fluffykins' }, | |
{ type: 'attack', value: 12, target: 'player-dorkman' }, | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const totalDamageOnDorkman = dragonEvents | |
.filter(function (event) { | |
return event.type === 'attack' | |
}) | |
.filter(function (event) { | |
return event.target === 'player-dorkman' | |
}) | |
.map(function(event) { | |
return event.value | |
}) | |
.reduce(function(prev, value) { | |
return (prev || 0) + value | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const totalDamageOnDorkman = dragonEvents | |
.filter(e => e.type === 'attack') | |
.filter(e => e.target === 'player-dorkman') | |
.map(e => e.value) | |
.reduce((prev, x) => (prev || 0) + x) |
running this exactly produce error:
.reduce(function(prev, value) {
^
TypeError: Reduce of empty array with no initial value
at Array.reduce (native)
at Object.<anonymous> (C:\BackendServicesSandbox\dragons.js:31:4)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:141:18)
at node.js:933:3
Any clue ?
by the way, my platform is: node.js v.5.7.0
on Windows 8.1
sorry, my bad. The issue was due to a typo.
Why .reduce(summerInitialValue) does not work?
const dragonEvents = [
{type: 'attack', value: 40, victim: 'dorkman'},
{type: 'sleep', value: 10},
{type: 'attack', value: 20, victim: 'aquaman'},
{type: 'attack', value: 40, victim: 'dorkman'},
]
const v = 'dorkman';
const field = 'victim';
const selectVictim = (e) => e[field] === v;
const selectAttack = e => e.type === 'attack';
const initialValue = 9
const summer = (total, v) => ( total + v) ;
const summerInitialValue = (initialValue) => (summer, initialValue)
const myReduce = (reducer, initialValue) => (summer, initialValue)
const totalDamageOnDorkman = dragonEvents
.filter(selectAttack)
.filter(selectVictim)
.map(e => e.value)
.reduce(summer,4) //works
// Does not work .reduce(summerInitialValue)
// Does not work [myReduce]
console.log(totalDamageOnDorkman)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hej, why don't you use the initial value parameter for
reduce
?