Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. function findLongestWord(str) {
  2. //i create two different empty arrays, because
  3. //1. store each word from a string in one array
  4. //2. store the lengths of each item of the array inside a new array
  5. //i return the largest number from the array which contains the lengths of the previous array
  6. var arr=[];
  7. var newarr=[];
  8. //this stores the words inside the string inside an array
  9. arr=str.split(" ")
  10. for (var i=0; i<arr.length; i++){
  11. //this stores the lengths of the words indide the new array
  12. newarr[i]=arr[i].length;
  13.  
  14. }
  15. //this gets the biggest value inside the new array
  16. return Math.max.apply(null,newarr);
  17. }
  18.  
  19. findLongestWord("The quick brown fox jumped over the lazy dog");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement