I am new to programming and javascript.
I am struggling to understand how to make this code print out all the strings that are longer in the array.
I want to write a function that takes in an array of strings and then prints out the longest one
for example: longestWord(["the","fresh","green", "dog", "chased", "my", "chickens"])
should print:
chickens
And If there are multiple longest strings then print them all in a different line.
for example: longestWord(["what", "about", "elephants", "and", "a", "crocodile"])
should print:
elephants
crocodile
What I have tried:
let array = ["Lions", "are", "incredibly", "elephant", "orange", "genuinelys"];
let sorted = array.sort(function (a, b){
return b.length - a.length;
});
console.log(sorted[0]);
//This prints out only "incredibly"