gpt4 book ai didi

javascript - 打印图案的功能

转载 作者:行者123 更新时间:2023-11-30 15:34:01 25 4
gpt4 key购买 nike

我卡住了。我需要找出应该打印“*****”的特定函数。

例如:

如果参数是 5 它应该打印 (five) ***** ,如果参数是 3 它应该打印 (three) *** 等

function line(n) {
return n;
}

console.log(line(5));

我非常感谢任何答案。 :)

最佳答案

您需要使用循环。它会一直工作到 n。请参阅代码中的注释。

function line(n) {
var stars = ''; // Declare an empty string

for(var i = 0; i < n; i++){ // Loop until i is less than n;
stars += '*' ; // add * to the stars in every iteration
}

return stars; // return stars
}

console.log(line(5));
console.log(line(3));

关于javascript - 打印图案的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41853082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com