本文介绍了Javascript循环返回字符串x次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在正在进行Udacity javascript研究,并且有一个令我困扰的测验。我知道如何在ruby中轻松完成,但是这个就是杀了我。
I'm doing Udacity javascript studies now and there is one quiz that bothers me. I know how to do it easily in ruby, but this one is killing me.
我需要调用函数并返回hanum次并添加!最后是循环。
I need to call function and return "ha" num times and add "!" at the end with the loop.
我试过这个但是没有帮助。应该很简单。
I tried this but it didn't help. Should be very simple.
function laugh(num) {
for (var x = 0; num; x ++) {
return 'ha';
}
}
console.log(laugh(3));
推荐答案
实际上你甚至不需要循环。
Actually you don't even need the loop.
const laugh = num => 'ha'.repeat(num) + '!';
console.log(laugh(3));
console.log(laugh(5));
这篇关于Javascript循环返回字符串x次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!