javascript - To count length of each word in sentence by using jQuery ,and send the largest word's length -
this question has answer here:
- find longest word in string? 7 answers
function findlongestword(str) { var length = 0; var j; var newstr = str.split(" "); for(var = 0;i<15;i++){ var lentemp = newstr[i].length(); if( lentemp >length){ length === lentemp ; } } return length ; }; findlongestword("the quick brown fox jumped on lazy dog");
i want result length of word largest? i'm new jquery. can me sort this?i'm learning jquery , can't proceed further without finishing this.
you using ===
comparison operator need assignment operator =
. use =
instead of ===
.
so line length === lentemp ;
should length = lentemp ;
also 1 more thing length
not function in javascript can't use length()
remove braces , use .length
.
this complete snippet:
function findlongestword(str) { var length = 0; var j; var newstr = str.split(" "); console.log(newstr.length); for(var = 0;i<newstr.length;i++){ var lentemp = newstr[i].length; if( lentemp >length){ length = lentemp ; } } return length ; }; alert(findlongestword("the quick brown fox jumped on lazy dog"));
Comments
Post a Comment