我试图弄清楚如何创建一个简单的函数,该函数将接受2个参数并返回一个新字符串,该字符串将重复字符串一定次数,如第二个参数中所述。
我想让它返回hellohellohellohellohello但它只返回你好一次。
示例:
这是我到目前为止所做的,但它只返回一次问候
function repeat (arg1, arg2)
{
var counter = 0
while(counter < arg2)
return arg1
counter++
}
repeat("hello", 5);
答案 0 :(得分:1)
我不是JS的专家,但我花了大约10秒才知道这已经存在了。
"Hello".repeat(10)
易。
答案 1 :(得分:0)
我不会给你代码,因为你应该自己尝试一下。但我会告诉你如何运作的逻辑。
function(arg1, arg2)
{
counter = 0
while(counter < arg2)
print arg1
counter++
}