JavaScript Javascript reduce() to find the shortest word in a string javascript reduce find max,javascript reduce find min,javascript reduce

I have a function that finds the longest word in a string.

function findLongestWord(str) {
  var longest = str.split(' ').reduce((longestWord, currentWord) =>{
    return currentWord.length > longestWord.length ? currentWord : longestWord;
  }, "");
  return longest;
}
console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));

I'm having a hard time converting this to find the shortest word. Why can't I just change currentWord.length > longestWord.length to currentWord.length < longestWord.length?

Answer:1

You need to provide an initial value to the reduce function, otherwise a blank string is the shortest word:

function findShortestWord(str) {
  var words = str.split(' ');
  var shortest = words.reduce((shortestWord, currentWord) => {
    return currentWord.length < shortestWord.length ? currentWord : shortestWord;
  }, words[0]);
  return shortest;
}
console.log(findShortestWord("The quick brown fox jumped over the lazy dog"));
Answer:2

While using reduce, initialValue is optional and if it isn't provided then your first element will be used as initialValue. So, in your case, you'll just have to remove your "":

function findLongestWord(str) {
  var longest = (typeof str == 'string'? str : '')
    .split(' ').reduce((longestWord, currentWord) =>{
      return currentWord.length < longestWord.length ? currentWord : longestWord;
  });
  return longest;
}
console.log(findLongestWord("The quick brown fox jumped over the lazy dog")); // The
Answer:3

I've been trying to access any script in my ASP.NET (Web Forms) application but it doesn't work. I'm able to access the file through the server therefore the destination is fine but the file doesnt ...

I've been trying to access any script in my ASP.NET (Web Forms) application but it doesn't work. I'm able to access the file through the server therefore the destination is fine but the file doesnt ...

I'm getting small issue, once we click on button one modal popup displaying, but at the time of unload the overlay doesn't disappear. I tried with below code, could anyone suggest me, whats the issue ...

I'm getting small issue, once we click on button one modal popup displaying, but at the time of unload the overlay doesn't disappear. I tried with below code, could anyone suggest me, whats the issue ...

  1. overlay disappear on hover
  2. beamer overlay disappear

I can't figure out how to convert async await functionality in a while loop to a promise based implementation. repl showing the async await version https://repl.it/repls/IdealisticPepperyCoding ...

I can't figure out how to convert async await functionality in a while loop to a promise based implementation. repl showing the async await version https://repl.it/repls/IdealisticPepperyCoding ...

  1. convert async await to promise
  2. convert async await to callback
  3. convert async await to observable
  4. convert to async await
  5. babel convert async await
  6. convert then to async await
  7. convert promise to async await online
  8. convert callback to async await javascript
  9. convert promise into async await
  10. convert callback to async await nodejs
  11. convert callback to async await c#
  12. convert callback function to async await
  13. convert promise.all to async await
  14. convert promise chain to async await
  15. convert then catch to async await
  16. convert new promise to async await

I want to navigate to another page in ASP.NET MVC. #second responds to the div of page and #first is the first div. I need to know how can I call this method on View page. $(document).on('click', '....

I want to navigate to another page in ASP.NET MVC. #second responds to the div of page and #first is the first div. I need to know how can I call this method on View page. $(document).on('click', '....

  1. calling jquery function from javascript
  2. calling jquery function in html
  3. calling jquery function on button click
  4. calling jquery function from c#
  5. calling jquery function from php
  6. calling jquery function
  7. calling jquery function on textbox change event
  8. calling jquery function from controller in mvc
  9. calling jquery function on page load
  10. calling jquery function from another file
  11. calling jquery function from code behind c#
  12. calling jquery function with parameters
  13. calling jquery function in angularjs
  14. calling jquery function from asp.net code behind
  15. calling jquery function from c# mvc
  16. calling jquery function from jsp
  17. calling jquery function on load
  18. calling jquery function from anchor tag
  19. jquery calling function multiple times
  20. jquery calling function on click