最后一个值未存储在Javascript变量中

最后一个值未存储在Javascript变量中,javascript,jquery,arrays,Javascript,Jquery,Arrays,我正在学习Dash上的Mad Lib教程,但遇到了以下问题: 每当我运行以下代码时,它都应该将我的所有三个输入存储在数组answers中,但是,它不存储最后一个输入。每当我用showFinal输出answers数组时,它会在第三个答案应该在的位置输出undefined。是一个JSFIDLE,输出相同的问题 下面是Javascript: // List of prompts for the user var prompts = [ 'Type your name', 'Type a

我正在学习Dash上的Mad Lib教程,但遇到了以下问题:

每当我运行以下代码时,它都应该将我的所有三个输入存储在数组
answers
中,但是,它不存储最后一个输入。每当我用
showFinal
输出
answers
数组时,它会在第三个答案应该在的位置输出
undefined
。是一个JSFIDLE,输出相同的问题

下面是Javascript:

// List of prompts for the user
var prompts = [
    'Type your name',
    'Type an adjective',
    'Type a noun'
   ];

var answers = [];
// Keep track of current prompt we're on
var currentPrompt = 0;

// A function that will call the next prompt
var nextPrompt = function() {
    // if there is a next prompt
    if (currentPrompt < prompts.length) {
      if (currentPrompt != 0) {
        answers.push($('input').val());
      }
        // put first prompt in all html elements with class 
        $('.prompt').html(prompts[currentPrompt] + '<br><input type="text">');
        // move the next prompt into variable currentPrompt 
        currentPrompt = currentPrompt + 1;
    }
    //or else if we're at the end of the array
    else {
        // put a new message into the html.
        showFinal();
    }
}

var showFinal = function() {
  $('.prompt').html(answers[0]+ ' ' + answers[1] + ' ' + answers[2]);
}

// run nextPrompt function when button is clicked
$('button').click(function() {
    nextPrompt();
});

// Show the first prompt as soon as js loads
nextPrompt();
//用户提示列表
变量提示=[
'键入您的姓名',
“键入一个形容词”,
“输入一个名词”
];
var回答=[];
//跟踪我们当前的提示
var currentPrompt=0;
//将调用下一个提示符的函数
var nextPrompt=函数(){
//如果有下一个提示
如果(currentPrompt');
//将下一个提示移到变量currentPrompt中
currentPrompt=currentPrompt+1;
}
//或者如果我们在数组的末尾
否则{
//将新消息放入html。
showFinal();
}
}
var showFinal=函数(){
$('.prompt').html(答案[0]+''+答案[1]+''+答案[2]);
}
//单击按钮时运行NextCompt函数
$(“按钮”)。单击(函数(){
nextPrompt();
});
//js加载后立即显示第一个提示
nextPrompt();
试试这个:

// List of prompts for the user
var prompts = [
    'Type your name',
    'Type an adjective',
    'Type a noun'];

var answers = [];
// Keep track of current prompt we're on
var currentPrompt = 0;

// A function that will call the next prompt
var nextPrompt = function () {
    // if there is a next prompt
    if (currentPrompt > 0) {
            answers.push($('input').val());
    }

    if (currentPrompt < prompts.length) {        
        // put first prompt in all html elements with class 
        $('.prompt').html(prompts[currentPrompt] + '<br><input type="text">');
        // move the next prompt into variable currentPrompt 
        currentPrompt++;
    }
    //or else if we're at the end of the array
    else {
        // put a new message into the html.
        showFinal();
    }
}

var showFinal = function () {
    $('.prompt').html(answers[0] + ' ' + answers[1] + ' ' + answers[2]);
}

// run nextPrompt function when button is clicked
$('button').click(function () {
    nextPrompt();
});

// Show the first prompt as soon as js loads
nextPrompt();
编辑:


作为对评论的回应,我使用了
>0
,因为它对我来说更有意义。通过使用
!=0
您暗示
currentPrompt
可以是小于零的值,例如
-1
。这是主观的,但任何一种方法都适用于您,这只是个人的事情:)

问题在于,当您单击“下一步”时,在保存当前值之前检查是否有更多提示消息。但如果没有进一步的提示消息,它将跳转到showFinal()

在检查是否有更多消息要提示之前,需要按下当前值

if (currentPrompt != 0) {
        answers.push($('input').val());
    }
// if there is a next prompt
if (currentPrompt < prompts.length) {
        ....
if(currentPrompt!=0){
push($('input').val());
}
//如果有下一个提示
如果(currentPrompt
更新的小提琴:

试试这个

//用户提示列表
变量提示=[
'键入您的姓名',
“键入一个形容词”,
“输入一个名词”];
var回答=[];
//跟踪我们当前的提示
var currentPrompt=0;
//将调用下一个提示符的函数
var nextPrompt=函数(){
//如果有下一个提示

if(currentPrompt)需要在
showFinal()
之前推送最后一个输入值。提示在最后一个答案上处于限制状态,它们跳过了推送答案,但用户刚刚键入了是,我看到我将
if(currentPrompt!=0){
放错地方了。奇怪的是,系统没有把它当成错误。谢谢!你想骗我问更多问题吗?:P它是
currentPrompt!=0
,而不是
;)
if (currentPrompt != 0) {
        answers.push($('input').val());
    }
// if there is a next prompt
if (currentPrompt < prompts.length) {
        ....
// List of prompts for the user
var prompts = [
    'Type your name',
    'Type an adjective',
    'Type a noun'];

var answers = [];
// Keep track of current prompt we're on
var currentPrompt = 0;

// A function that will call the next prompt
var nextPrompt = function () {
    // if there is a next prompt
    if (currentPrompt <= prompts.length) {
        if (currentPrompt != 0) {
            console.log($('input').val()+'...');
            answers.push($('input').val());
        }
        // put first prompt in all html elements with class 
        if (currentPrompt != prompts.length) {
        $('.prompt').html(prompts[currentPrompt] + '<br><input type="text">');
        }else{
             showFinal();
        }
        // move the next prompt into variable currentPrompt 
        currentPrompt = currentPrompt + 1;
    }

}

var showFinal = function () {
    $('.prompt').html(answers[0] + ' ' + answers[1] + ' ' + answers[2]);
}

// run nextPrompt function when button is clicked
$('button').click(function () {
    nextPrompt();
});

// Show the first prompt as soon as js loads
nextPrompt();