DEV Community

Discussion on: Understanding reduce in javascript

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Reduce is really useful. "Write a function to convert a binary number into decimal without using parseInt" - OK...

const binToDec = str => [...str].reduce((a,v)=>+v+a*2,0)

console.log(binToDec('11101')) // 29
Enter fullscreen mode Exit fullscreen mode
Collapse
 
wulforr profile image
Shaurya Vardhan Singh

Thats really awesome Jon. Using reduce in this way never crossed my mind, this really opens up many possibilities