codecamp

设置首字母大写算法挑战

方法一:

function titleCase(str) {
//拆分为数组
  var arr = str.toLowerCase().split(/\s/g);
  //遍历数组
  for(var i=0;i<arr.length;i++){
  //选取数组每一项第一个字母转换为大写
    arr[i] = arr[i].slice(0, 1).toUpperCase() + arr[i].slice(1);  
  }
  //转换为字符串
return arr.join(' ');
}
titleCase("I'm a little tea pot");

方法二:

function titleCase(str) {
  str = str.toLowerCase();
  var arr = str.split(' ');
  for(var i=0;i<arr.length;i++){
    var tem = arr[i];
    arr[i] = tem.substring(0,1).toUpperCase() + tem.substring(1);   
  }
  return arr.join(' ');
}
寻找最长的单词算法挑战
寻找数组中的最大值算法挑战
温馨提示
下载编程狮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; }