DEV Community

Discussion on: Solving For Binary Gap Using JavaScript

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited
const solution = i=>Math.max(...i.toString(2).split(/^0+|1+|0+$/).map(s=>s.length))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kyrolosmagdy profile image
Kyrolos

I liked this one, Thanks for sharing Dude.

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Shorter:

const solution = i=>i.toString(2).split(/^0+|1+|0+$/).sort().at(-1).length
Enter fullscreen mode Exit fullscreen mode