Joys of Javascript
2024-09-12
Not familiar w/ JavaScript? Here's your crash course!
// arrow syntax => assigns anonymous functions
const ;
// each iteration of reduce() the `acc` is returned for 'further processing'
const = ;
const = 23;
const = ;
// and since this isn't enough fun yet:
// reduce's callback function takes _ANY_ number of parameters
// undefined parameters are simply left undefined
// this is JS being flexible (the function is _not_ overloaded)
;
// you cannot only provide b, but can simulate this effect
// (of course we can simply reuse the function name 'example' cause...)
;
// 'undefined' is a special value representing value abscence
// back to reduce:
// a) illustration
const = ;
const = ;
// b) leaving last two parameters undefined
const = ;
; // 2 + 4 + 6 + 8
Output:
Target 23 found at index 2
a: 1
b: undefined
a: undefined
b: 2
acc: 0 val: 2 idx: 0 arr: 2,4,6,8
acc: 0 val: 4 idx: 1 arr: 2,4,6,8
acc: 0 val: 6 idx: 2 arr: 2,4,6,8
acc: 0 val: 8 idx: 3 arr: 2,4,6,8
20