gpt4 book ai didi

javascript - 用Js实现有趣的游戏

转载 作者:行者123 更新时间:2023-11-30 11:57:26 24 4
gpt4 key购买 nike

我正在尝试实现一个功能,该功能要求多个 qs 存储它们,并用这些输入返回有趣的句子。但它不起作用,没有显示错误,但列表没有保存用户本身的 3 个输入,并注意到每次将输入保存在 alist 数组中时..它会替换以前的列表项,但我想保存所有这三个。

for(i = 0; i < 3; i++) {
var qlist = ["adjective?","Verb?", "noun?"];
var alist = [];
alist[i] = prompt(qlist[i] + "[questions number-" + i + "]");
}
var statement = document.write("There once was a "+ alist[0] + " programmer who once wanted to use javascript to " + alist[1] + " the " + alist[2]);
alert(statement);

最佳答案

alist 需要在循环外初始化。否则你会覆盖它。

另外,无论哪种方式,都不要警告(声明)类型未定义。

另外,不要初始化 qlist 3 次。

let alist = [];
let qlist = ["adjective?","Verb?", "noun?"];
for(let i = 0; i < 3; i++) {
alist[i] = prompt(qlist[i] + "[questions number-" + i + "]");
}
let statement = document.write("There once was a "+ alist[0] + " programmer who once wanted to use javascript to " + alist[1] + " the " + alist[2]);

关于javascript - 用Js实现有趣的游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37531466/

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