2014-09-16 1 views
0

자바 스크립트 변수에 저장되지 값, 그러나 나는 다음과 같은 문제로 실행 : 나는 다음과 같은 코드를 실행 할 때마다마지막으로 내가 대시에 미친 해방 자습서를 다음있어

는, 그것은 내 입력의 세 가지를 모두 저장해야합니다 배열 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(); 
+0

은'showFinal()'전에 마지막 입력 값을 밀어 넣어야합니다. 프롬프트는 마지막 답변에 한계가 있으며 답을 푸는 대신 건너 뜁니다. 사용자가 방금 입력했습니다 – charlietfl

답변

1

문제는 모든 단계에서 '다음'을 클릭하면 현재 값을 저장하기 전에 프롬프트 메시지가 더 있는지 확인합니다. http://jsfiddle.net/w840jpac/1/

+0

아, 제가'if (currentPrompt! = 0) {'를 잘못된 장소에 놓았습니다. 이상하게도 시스템이 실수로 생각하지 않았습니다. 감사! – Rvervuurt

0

보십시오이 :

// 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(); 

기본적으로 내가 더() 함수 nextPrompt에서이 부분을 이동 :

if (currentPrompt > 0) { 
    answers.push($('input').val()); 
} 

편집 :

덧글에 대한 응답으로, 나는 그것이 더 나에게 의미가 있기 때문에 단순히 > 0을 사용합니다. != 0을 사용하면 currentPrompt이 0보다 작은 값일 수 있음을 암시 적으로 나타냅니다. -1. 그것은 주관적이지만 어떤 방법이든 당신을 위해 일할 것입니다, 그것은 단지 개인적인 것입니다 :)

+0

더 많은 질문을하기 위해 속이려고하십니까? : P 그것은'>'가 아니라'currentPrompt! = 0'였습니다;) – Rvervuurt

1

보십시오 : 그것은 showFinal()

으로 건너 뜁니다 더 이상 프롬프트 메시지가없는 경우 그러나 당신은

if (currentPrompt != 0) { 
     answers.push($('input').val()); 
    } 
// if there is a next prompt 
if (currentPrompt < prompts.length) { 
     .... 

업데이트 바이올린을 묻는 메시지를 표시하도록 더 많은 메시지가있는 경우 확인하기 전에 현재의 값을 밀어 필요 이 .. DEMO

// 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(); 
관련 문제