Ask Your Question
0

What is the smallest word in a list using JS?

asked 2021-11-09 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-04-19 03:00:00 +0000

devzero gravatar image

To find the smallest word in a list using JS, you can use the following code:

const words = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
const smallestWord = words.reduce((prev, curr) => prev.length < curr.length ? prev : curr);
console.log(smallestWord); // Output: 'date'

In the above code, we have an array of words and we use the reduce() method to iterate through the array and compare the length of each word in the array. The reduce() method returns the smallest word in the array by comparing the length of the current word with the previous word. The output of this code will be the smallest word in the list which is 'date'.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-09 11:00:00 +0000

Seen: 16 times

Last updated: Apr 19 '21