Hi. I’m not still understanding what ‘x’ and ‘y’ each indicate in the reduce function below. This code is from the “Find the Longest Word in a StringPassed” in the Basic Algorithm Scripting.

Does the ‘x’ mean each word splited by .split(’ ')?
And why doesn’t x have ’ .length ’ while y does?
I’m so confused.

Disclaimer: I learned .reduce(). I read everything I could. But it still doesn’t make sense to me. So can somebody please just kindly explain to me what the x and y are in this Longest Word context? Thanks!

Your code so far

function findLongestWordLength(s) {
return s.split(' ')
.reduce(function(x, y) {
return Math.max(x, y.length)
}, 0);
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");

Hello,
To understand the code better, might be a good idea to choose more descriptive names for variables, I memorized reduce() callback arguments as all,item,index

function findLongestWordLength(s) {
return s.split(' ')
.reduce(function(all, item) {
console.log(`all: ${all} item.length: ${item.length}`); // console.log to see values of variables
return Math.max(all, item.length)//compares all with each item.length of given array , returns maximum of two, assigns it to ```all```
}, 0); //all is initialized with 0
};
console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"))

Your reducer function’s returned value is assigned to the accumulator, whose value is remembered across each iteration throughout the array, and ultimately becomes the final, single resulting value.MDN

The split method divides a string into an ordered set of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call. in this case the pattern provided checks for ' 'so it splits the string into individual words because this pattern is found between each word.

This is what what happens to the the string

“The quick brown fox jumped over the lazy dog”.

Each individual words becomes a substring of an array.

Then the reduce method executes a reducer function (that you provide) on each element of the array, resulting in single output value. Now here is the part where the x and y is relevant. Your reducer function’s returned value is assigned to the accumulator, whose value is remembered across each iteration throughout the array, and ultimately becomes the final, single resulting value. **The accumulator is the x **. The y is the current element being processed in the array.

A key part of this function is Math.max(). The Math.max() function returns the largest of the zero or more numbers given as input parameters.

In this case it is comparing the y value to the x value. That means it compares the length of the substring it is currently looking at (y) to the previously returned value (x) also called the accumulator .

Because we are using the reduce method, we are able to use the Math.max() for each element in the array, ie checking the largest number between the accumulator(x) and the current length of the substring in the array (y) so by the end the value of the accumulator(x) will be whichever substring had the longest length.