User Tools

Site Tools


java-script:reduce:find-longest-word-from-a-string

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
java-script:reduce:find-longest-word-from-a-string [2023/08/07 21:03] odeftajava-script:reduce:find-longest-word-from-a-string [2023/08/07 21:04] (current) odefta
Line 6: Line 6:
  
 <note> <note>
-The function then uses the reduce method on the array of words to find the longest word. The reduce method takes a callback function that compares the length of the current word to the length of the longest word found so far (maxWord):+The function uses the **reduce** method on the array of words to find the longest word. The reduce method takes a callback function that compares the length of the current word to the length of the longest word found so far (maxWord):
 <code javascript> <code javascript>
 .reduce((maxWord, word) => { .reduce((maxWord, word) => {
Line 12: Line 12:
 }, '') }, '')
 </code> </code>
- 
-Here's how the reduce method works: 
- 
-  * **maxWord**: The accumulator that keeps track of the longest word found so far. It is initialized with an empty string '' 
-  * **word**: The current word being processed in the array. 
-  * **The ternary operator (condition) ?** valueIfTrue : valueIfFalse is used to compare the length of the current word with the length of maxWord. If the current word is longer, it becomes the new value of maxWord; otherwise, maxWord remains unchanged. 
-  * The final value of **maxWord**, once all the words in the array have been processed, is the longest word, and that's what the reduce method returns. 
 </note> </note>
  
java-script/reduce/find-longest-word-from-a-string.1691431398.txt.gz · Last modified: 2023/08/07 21:03 by odefta