자바 스크립트 변수에 저장되지 값, 그러나 나는 다음과 같은 문제로 실행 : 나는 다음과 같은 코드를 실행 할 때마다마지막으로 내가 대시에 미친 해방 자습서를 다음있어
는, 그것은 내 입력의 세 가지를 모두 저장해야합니다 배열 answers
, 그러나 마지막 한 저장하지 않습니다. showFinal
을 사용하여 answers
어레이를 출력 할 때마다 세 번째 응답이 있어야하는 곳의 undefined
을 출력합니다. Here은 jsfiddle이며 같은 문제가 발생합니다. 여기
// 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();
은'showFinal()'전에 마지막 입력 값을 밀어 넣어야합니다. 프롬프트는 마지막 답변에 한계가 있으며 답을 푸는 대신 건너 뜁니다. 사용자가 방금 입력했습니다 – charlietfl