-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestString.js
More file actions
20 lines (17 loc) · 890 Bytes
/
Copy pathlongestString.js
File metadata and controls
20 lines (17 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Define a function that takes a string as an argument
function findLongestWord(str) {
// Splits the given string and puts the results into an array
var strSplit = str.split(' ');
/* Takes the split string and sorts it alphabetically using a compare function
which compares two values (x, y) and depending on the rules set in the return statement
decides which value goes first, second, etc. */
var longest = strSplit.sort(function(x, y) {
/* Returns the length of the 2 function arguments. Depending on the length
of each string, the compare function determines the order of items in the array */
return y.length - x.length;
});
// Returns the longest string starting from the [0] index
return longest[0].length;
}
// Call the findLongestWord function with a strng as an argument
findLongestWord("This is the string that will be tested.");