127k views
3 votes
function getLongestString(strings) { } /* Do not modify code below this line */ const strings = ['long', 'longer', 'longest']; console.log(getLongestString(strings), '<-- should be "longest"');

1 Answer

4 votes

Answer:

function getLongestString(strings) {

return strings.reduce( (acc, cur) => acc.length > cur.length ? acc : cur);

}

Step-by-step explanation:

A reducer applies the same operation to each array element. In this case, the longest string is stored in the accumulator (acc), and is replaced only by the current element (cur) if the current element is longer than the accumulator.

User Blownhither Ma
by
8.3k points