3 ms·
For point 1 I would use "reduce": lines.reduce((longest, line) => (longest.length > line.length) ? longest : line, "") https://developer.mozilla.org/en/docs
by gumoro 10y ago
For point 1 I would use "reduce":
lines.reduce((longest, line) => (longest.length > line.length) ? longest : line, "")
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce https://developer.mozilla.org/en/docs/Web/JavaScript/Referen...
EDIT: sorry, initial code returned length, not line itself
- nunull 10y agoThis returns the length, not the line itself. Edit: `lines.reduce((line, line_) => line_.length > line.length ? line_ : line, '')`
- gumoro 10y agoThanks, edited.
- paradite 10y agoIt is a good use of reduce (accumulate as I call it), but maybe slightly overkill for this case compared to a simple iteration or a max function.