codecamp

重复操作算法挑战

方法一:

function repeat(str, num) {
// repeat after me
var temp="";
  for(var i=0;i<num;i++){
    temp += str;
  }
return temp;
}
repeat("abc", 3);

方法二:

function repeat(str, num) {
  var tem = str;
  // repeat after me
  if(num<0){
    str = '';
  } else {
    for(var i=1;i<num;i++){
      str += tem;
    }
  }
  return str;
}


repeat("abc", 3);
确认末尾字符算法挑战
字符串截取算法挑战
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定