Skip to content
Exercise · Array methods · lesson 10 of 12

Exercise: the longest word

Write a function function longestWord(words) that returns the longest word in the array words.

  • longestWord(["sun", "planet", "moon"]) → "planet"
  • longestWord(["cat", "dog"]) → "cat"
  • longestWord([]) → ""

Constraints: words holds 0 to 10 non-empty strings; if several words are the longest, return the one that comes first; for an empty array return the empty string "".

Do not add extra imports unless the task says so explicitly.

longest.js — javascript
javascript · 4 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way