寻找最长单词算法


用split函数把字符串分割成列表,然后判断列表中字符串的长度,进行比较。
function findLongestWord(str) {
    var str_list = str.split(" ");
    var word_lenght = 0;
    for (var i=1; i < str_list.length; i++){
        if (str_list[i].length > word_lenght) {
            word_lenght = str_list[i].length;
        }
    }
        
    return word_lenght;
}

文章作者: theing
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 theing !
评论
  目录