zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Repeat a string repeat a string #296

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

挑战

如果num是正数,则字符串重复num次。

代码


function repeatStringNumTimes(str, num) {
  // repeat after me

  var strNum=str;

  if(num>0){
    for(var i=1;i<num;i++){
        str+=strNum; 
    }
    return str;

  }else{
    return "";
  }

}

repeatStringNumTimes("abc", 3);

结果显示

image

帮助

The String global object

来源

https://www.freecodecamp.org/challenges/repeat-a-string-repeat-a-string

zilongxuan001 commented 6 years ago

经验谈20180328

这道题比较简单,但一开始容易做错。

为什么JavaScript不能用str*3呀?为什么str不能乘以一个数字,这在python里是很正常的呀。