Longest Word In A String Using Map And Math Max

Problem Statement

Return the length of the longest word in the provided sentence.

Solution

This is my second solution after I asked for feedback on my original solution where I used a reduce method. In this solution, first we split the sentence into an array of words using the str.split() method, which seperates the sentence by the spaces in it (we passed in a space as an argument).

Then we use Math.max to return the biggest number in an array of numbers. We used the spread operator to expand our passed in array into individual array elements. We created our array of numbers inside the math.max method, using a map method on our words array.

The map method calls a function on every element in an array, and creates a new array the same length of the original array with the transformed values. In this case, we are creating a new array with the word lengths of each or the words in our original array. The math.max function returns the biggest of these numbers - so only a single result is returned.

I posted my original solution on Twitter, and a friend pointed out that this wouldn't work for sentences that have non-alphanumeric characters. So happy they pointed that out, so added a regex to remove non-alphanumeric characters before splitting into words.

``````
function findLongestWordLength(str) {

str.replace(/\W/g, '');
const words = str.split(" ");

return Math.max(...words.map(word => word.length));
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");
```
```

The first time I solved this problem, I used a reduce function. The reduce function lets you run another function on each element in your array, and then provide a single output.

In this solution, first we split the sentence into an array of words using the str.split() method, which seperates the sentence by the spaces in it (we passed in a space as an argument).

Then, we create a variable called 'longest_word', we we store the result of our reduce function, where we passed in our own function which returns the length of the longest word encountered so far. This function is run on every word in our array, and the longest word length is output at the end.

``````
function findLongestWordLength(str) {

const words = str.split(" ");

const longest_word = words.reduce(function(word1,word2) {
return word1.length > word2.length ? word1 : word2;
});

return longest_word.length;
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");
```
```

Largest number in each sub array

In the kata immediately following the one above, I was able to apply what I just learned to the new problem, which was an extremely satisfying feeling.

Problem statement

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].

Solution

``````
function largestOfFour(arr) {
return arr.map(largest_num => Math.max(...largest_num));
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
```
```