您的位置:首页 > 其它

Find the Longest Word in a String

2017-03-03 12:30 435 查看


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

Your response should be a number

思路:
1.使用split(" ")将字符串转换为字符串;
2.将数组的第一个数赋给b;
3.遍历数组,如果数组中的数的长度大于b,将此数赋给b
 for(var i=0;i<array.length;i++){
    if(b.length<array[i].length){
      b=array[i];
    }

4.返回b的长度  b.length;

function findLongestWord(str) {

  var array=str.split(" ");

  var b=array[0];

  for(var i=0;i<array.length;i++){

    if(b.length<array[i].length){

      b=array[i];

    }

    

  }

  return b.length;

}

findLongestWord("The quick brown fox jumped over the lazy dog");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐