DEV Community

Discussion on: Keeping Your Code Simple

Collapse
 
willvincent profile image
Will Vincent • Edited

Even if you're "just a web dev" your code really ought to be given the same amount of care as if it were responsible for people's lives. :)

But if you really want to be clever, and keep it readable, it can be dramatically shortened with lodash's maxBy method...

const _ = require('lodash')

const strings = [
  '12345',
  '123',
  '1234567',
  '12',
  '1',
  '123456',
  '1234',
]

const result = _.maxBy(strings, string => string.length)

I'm sure the code challenge requires no external libraries, but that's not necessarily anything like a real-world requirement.

Interestingly, lodash uses a for loop for this, for what it's worth..