gpt4 book ai didi

javascript - 如何创建一个循环来显示还有多少问题需要回答?

转载 作者:行者123 更新时间:2023-12-03 11:32:06 25 4
gpt4 key购买 nike

我正在尝试将此代码转换为循环:

var questions = 3;
var questionsLeft = '[ ' + questions + ' questions left]';
var question1 = prompt('Whats your name? ' + questionsLeft);
questions -= 1;
var questionsLeft = '[ ' + questions + ' questions left]';
var question2 = prompt('Whats your age? ' + questionsLeft);
questions -=1
var questionsLeft = '[ ' + questions + ' questions left]';
var question3 = prompt('Whats your dogs name? ' + questionsLeft);

所以,我这样做了:

var question1;
var question2;
var question3;
var questions = 3;

for(var i = 0; i < questions; --questions) {
var questionsLeft = '[' + questions + ' questions left]';

if (questions == 3) {
question1 = prompt('Whats your name? ' + questionsLeft);
}
if (questions == 2) {
question2 = prompt('Whats your age ' + questionsLeft);
}
if (questions == 1) {
question2 = prompt('Whats your dog\'s name? ' + questionsLeft);
}
}

我认为这是错误的方式,对吗?我怎样才能做到正确?

<小时/>

更新:

var question = ['Whats your name? ', 'Whats your age?', 'Whats your color?'];
var questions = 3;

for(var i = 0; i < questions; i++) {
var questionsLeft = ' [' + questions + ' questions left]';
alert(question[i] + questionsLeft);
}

现在它回来了:

你叫什么名字? [还剩3个问题]你几岁? [还剩2个问题]停止

最佳答案

为什么不简单地使用像这样的数组:

questions=["Whats your name?","Whats you age?","Whats your dog\'s name?"];
answers=[]
for (i=0; i<questions.length; i++) {
var j=3-i;
var questionsLeft = '[' + j + ' questions left]';
answers.push(prompt(questions[i] + " "+ questionsLeft));
}

您在 answers 数组中得到了所有答案。

关于javascript - 如何创建一个循环来显示还有多少问题需要回答?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26692443/

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