Skip to content

Instantly share code, notes, and snippets.

@dewey92
Last active February 28, 2019 01:50
Show Gist options
  • Save dewey92/12bed5efdf5361308c46e918f5d45429 to your computer and use it in GitHub Desktop.
Save dewey92/12bed5efdf5361308c46e918f5d45429 to your computer and use it in GitHub Desktop.
const wedhus = x => [hargaKambing, diskon10, times3, add2].reduceRight((acc, fn) => fn(acc), x)
wedhus(77) = [hargaKambing, diskon10, times3, add2].reduceRight((acc, fn) => fn(acc), 77)
// STEP BY STEP
// Iterasi 1: fn = add2, acc = 77
wedhus(77) = [hargaKambing, diskon10, times3].reduceRight((acc, fn) => add2(77), 77)
// Iterasi 2: fn = times3, acc = add2(77)
wedhus(77) = [hargaKambing, diskon10].reduceRight((acc, fn) => times3(add2(77)), 77)
// Iterasi 3: fn = diskon10, acc = times3(add2(77))
wedhus(77) = [hargaKambing].reduceRight((acc, fn) => diskon10(times3(add2(77))), 77)
// Iterasi 4: fn = hargaKambing, acc = diskon10(times3(add2(77)))
wedhus(77) = [].reduceRight((acc, fn) => hargaKambing(diskon10(times3(add2(77)))), 77)
// Iterasi 5 selesai, sehingga
wedhus(77) = hargaKambing(diskon10(times3(add2(77))))
console.log(wedhus(77))
// => "Jual kambing harga $213.3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment