Q:

Write a JavaScript program to take any number of iterable objects or objects with a length property and returns the longest one

0

Write a JavaScript program to take any number of iterable objects or objects with a length property and returns the longest one.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const longestItem = (...vals) => [...vals].sort((a, b) => b.length - a.length)[0];
console.log(longestItem('this', 'is', 'a', 'testcase'));
console.log(longestItem(...['a', 'ab', 'abc']));
console.log(longestItem(...['a', 'ab', 'abc'], 'abcd'));
console.log(longestItem([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]));
console.log(longestItem([1, 2, 3], 'foobar'));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now