String 如何在javascript循环中重复字符串
我试图编写一个函数,通过在该函数中创建一个循环,将字符串重复3次:String 如何在javascript循环中重复字符串,string,loops,repeat,String,Loops,Repeat,我试图编写一个函数,通过在该函数中创建一个循环,将字符串重复3次: repeatString('hey', 3) // returns 'heyheyhey' 到目前为止,我有下面的代码,并试图做一个循环 const repeatString = function() { } module.exports = repeatString 这就是我想出并使用的代码 const repeatString = function repeatString( str,
repeatString('hey', 3) // returns 'heyheyhey'
到目前为止,我有下面的代码,并试图做一个循环
const repeatString = function() {
}
module.exports = repeatString
这就是我想出并使用的代码
const repeatString = function repeatString( str, num) {
str ='hey';
if (num > 0) {
return str.repeat(num);
} else if (num < 0) {
return 'ERROR';
} else {
return '';
}
}
module.exports = repeatString
const repeatString=函数repeatString(str,num){
str='嘿';
如果(数值>0){
返回str.repeat(num);
}else if(num<0){
返回“错误”;
}否则{
返回“”;
}
}
module.exports=repeatString
有点干净
const repeatString=函数(str,num){
返回(num<0)?新错误(“错误”):str.repeat(num);
}
module.exports=repeatString
函数如何知道重复字符串的次数?它如何知道输入字符串是什么?看看JavaScript文档,了解如何定义函数以及如何编写简单循环。文档中的示例应该足以让您取得进展。
const repeatString = function(str, num) {
return (num < 0) ? new Error("Error") : str.repeat(num);
}
module.exports = repeatString