gpt4 book ai didi

Javascript 在函数后不断变得未定义 - 提示

转载 作者:行者123 更新时间:2023-12-02 16:29:55 26 4
gpt4 key购买 nike

所以我正在编写一个简单的 JS 代码。我们刚刚开始学习函数。我需要创建一个名为“printStars”的函数。

我需要从用户那里获取一个号码,并根据该号码打印“*”。

这就是我所做的:

    <script>   
function printStars()
{
var n = Number(prompt("Insert number of stars:","0.."));
for (i = 0; i < n; i++)
{
document.write("*");
}
}
var stars = printStars();
document.write(stars);
</script>

最后我得到的结果是“未定义”的减号。我很想获得一些帮助,并解释为什么这种情况不断发生。

谢谢大家!

最佳答案

jsfiddle demo

function printStars(){
var n = prompt("Insert number of stars:","0..");
var stars='';
for (i = 0; i < n; i++){
stars+='*';

}
$('body').html(stars) //jsfiddle does not allow document.write()
//document.write(stars);
}

//call the function
printStars();

关于Javascript 在函数后不断变得未定义 - 提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28399381/

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