美文网首页
Repeat a string repeat a string

Repeat a string repeat a string

作者: yyggfffg | 来源:发表于2018-04-21 10:51 被阅读0次

重复输出字符串

(重要的事情说3遍)

重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串

function repeat(str, num) {
  var newStr='';
  // 声明新变量为空字符串
  for(var i=0;i<num;i++){
      newStr+=str;  
  }
  // 在空字符串上,累加num次
  return newStr;
   // num在for循环条件内,则进行for循环中的累加,
   // num不在for循环条件,则直接返回空字符串;
}

相关文章

网友评论

      本文标题:Repeat a string repeat a string

      本文链接:https://www.haomeiwen.com/subject/qtztlftx.html