Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function findLongestWord(str) {
- //i create two different empty arrays, because
- //1. store each word from a string in one array
- //2. store the lengths of each item of the array inside a new array
- //i return the largest number from the array which contains the lengths of the previous array
- var arr=[];
- var newarr=[];
- //this stores the words inside the string inside an array
- arr=str.split(" ")
- for (var i=0; i<arr.length; i++){
- //this stores the lengths of the words indide the new array
- newarr[i]=arr[i].length;
- }
- //this gets the biggest value inside the new array
- return Math.max.apply(null,newarr);
- }
- findLongestWord("The quick brown fox jumped over the lazy dog");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement