Javascript String findLongestWord(str)

Description

Javascript String findLongestWord(str)


/*/*ww w  . j  a  va 2s .  c o  m*/

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

Your response should be a number.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:

String.prototype.split()
String.length

*/

function findLongestWord(str) {
  var sortedArray = str.split(" ").sort(function(current,next){
    return current.length < next.length;
  });
  return sortedArray[0].length;
}

console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));

Javascript String findLongestWord(str)

/*/*w w w .  ja  v  a2  s.co m*/
Return the length of the longest word in the provided sentence.
Your response should be a number.
Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
String.prototype.split()
String.length
*/

function findLongestWord(str) {

    str = str.split(" ");
    var longest = 0;
    var word;
    for (var i = 0; i < str.length; i++) {
        if (longest < str[i].length) {
            longest = str[i].length;
            word = str[i];
        }
    }
    return word.length;
}

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

Javascript String findLongestWord(str)

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

Your response should be a number.//from   w  w w  .j  a v a 2s.c  om

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:

String.prototype.split()
String.length*/


function findLongestWord(str) {
  var array = [];
  array = str.split(" ");
  var longest = 0;
  array.map(function(val){
      if(val.length > longest) {
          longest = val.length;
      }
      return longest;
  });
  return longest
}

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

Javascript String findLongestWord(str)

/*Find the Longest Word in a String 
Return the length of the longest word in the provided sentence.

Your response should be a number.//from ww  w .  j  ava2s .com

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:

String.prototype.split()
String.length*/
function findLongestWord(str) {
  var myArray=str.split(" ");
  var maxLength=1;
  for(var i=0; i<myArray.length;i++){
    var l=myArray[i].length;
    if(l>maxLength)
      maxLength=l;
  }
  return maxLength;
}

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

Javascript String findLongestWord(str)

/**//from w w w .  ja v  a 2  s .co m
 * Return the length of the longest word in the provided sentence.

Your response should be a number.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:

String.prototype.split()
String.length
 */

 function findLongestWord(str) {
    
    var formatedStr = str.split(' ');
    var max = 0;

    for (var i = 0; i != formatedStr.length; i++) {
        if (formatedStr[i].length > max) {
            max = formatedStr[i].length;       
        }
       
    }
    
    return max;
 }

 console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));



PreviousNext

Related