For trick number 5, forEach seems like a fine approach, but stylistically I'm more pulled towards Reduce since Javascript doesn't have a maxBy or equivalent.
const array = ["Apple", "Pine-apple", "Banana", "Jack-fruit"];
const longest = array.reduce((longest, current) => {
if (longest.length < current.length) {
return current;
}
return longest;
});
console.log(longest); // Pine-apple
Ternary version looks a lot cleaner, but are notoriously harder to read.
const array = ["Apple", "Pine-apple", "Banana", "Jack-fruit"];
const longest = array.reduce((longest, current) =>
current.length > longest.length ? current : longest
);
console.log(longest); // Pine-apple