codecamp

寻找最长的单词算法挑战

方法一:

function findLongestWord(str) {
  var arr = str.split(/\s/g);
   temp = arr[0].length;
    //遍历+判断到最后一位
  for(var i=1;i<arr.length;i++){
    if(arr[i].length>temp){
      temp = arr[i].length;
    }
  }
return temp;
}
findLongestWord("The quick brown fox jumped over the lazy dog");

方法二:

function findLongestWord(str) {
  var arr = str.split(' '),
      len = 0;
  for(var i=0;i<arr.length;i++) {
    var tLen = arr[i].length;
    if(tLen > len) {
      len = tLen;
    }
  }
  return len;
}


findLongestWord("The quick brown fox jumped over the lazy dog");
回文算法挑战
设置首字母大写算法挑战
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

HTML5&CSS

JavaScript

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }