DEV Community

Discussion on: Keeping Your Code Simple

Collapse
 
ptdecker profile image
P. Todd Decker

I don't get it. If you want to keep it simple, keep it simple. There is no need at all for lambdas or reduce functions here. Why not simple? Trying to get it all with single lines of code is silly. Anyway you cut it it is an O(n) problem.

function longestString(strs) {
  let longest = 0;
  for (let i = 0; i < strs.length; i++) {
    if (strs[i].length >= strs[longest].length) {
      longest = i;
    }
  }
  return strs[longest];
}

console.log(
  longestString(['hi', 'there', 'tiffany'])
)