2016-08-18 5 views
0

번역 게임을 만들 때 배열에서 임의로 단어를 선택하고 사용자가 올바른 대답을 선택하거나 단어를 전달하면 해당 단어가 배열에서 제거됩니다. 이것을 재생할 때 이전에 제거 된 단어가 무작위로 선택 될 것이고 제거 된 단어는 제거 된 사용자 응답을 확인할 필요가 없기 때문입니다.배열에서 항목을 제거했지만 항목이 돌아 오는 것 같습니다.

여기에 바이올린이 있습니다. 힌트 버튼을 사용하여 정답으로 재생하고 테스트 할 수 있습니다. 여기

은 JS 바이올린의 URL의 끝에 추가 할 수있는 링크입니다 - # & togetherjs = U713xeqrK3 "여기

는 자바 스크립트입니다

var words = 
    [["Good morning", "buenos días"], 
    ["Good afternoon", "buenas tardes"], 
    ["Good evening", "buenas noches"], 
    ["Hello, my name is John", "hola me llamo juan"], 
    ["What is your name", "como te llamas"], 
    ["I am fine", "estoy bien"] 
    ["Nice to meet you", "mucho gusto"], 
    ["I'm sorry", "lo siento"], 
    ["Bless you", "salud"], 
    ["You are welcome", "de nada"], 
    ["I do not understand", "yo no comprendo"], 
    ["I love you", "te amo"], 
    ["Call the police!", "llame a la policía"], 
    ["What time is it?", "que hora es"], 
    ["Do you understand?", "entiende"], 
    ["I need", "yo necesito"], 
    ["How much does it cost", "cuanto cuesta"]]; 

    var randomNumber; 
    var count = 0; 
    var wordsSum = words.length ; 
    var passCount; 
    var consec = 0; 

$(document).ready(function(){ 

    $('#submit').click(function(){ 
    var choice = $('#value').val().toLowerCase(); 
    if (choice == words[randomNumber][1]) 
    { 
     $('#result').text("You are correct, well done"); 
     $('#submit').hide(); 
     $('#next').show();  
    } 
    else 
    { 
     $('#value').val(''); 
     $('#result').text("That is incorrect, try again"); 
     consec = 0; 
     $('#score').text(count); 
     $('#pass').show(); 
    } 
}); 

$('#next').click(function(){ 
    words.splice(randomNumber, 1); 
    count = count + 1; 
    consec = consec + 1; 
    $('#score').text(consec); 
    nextQuestion();    
}); 

$('#pass').click(function(){ 
    alert(words); 
      words.splice(randomNumber, 1); 
      count = count + 1; 
      consec = 0; 
      nextQuestion();  
     }); 

function nextQuestion(){ 
    if (words.length == 0) 
    { 
     $('#bar').css('width', count * 2/wordsSum * 100); 
     $('#percentage').text(Math.floor(count/wordsSum * 100) + ' %'); 
     count = count - 2; 
     $('#englishWord').text('The End'); 
     $('#again').show(); 
     $('#submit').hide(); 
     $('#next').hide(); 
     $('#value').val(''); 
     $('#pass').hide(); 
    } 
    else 
    { 

    $('#bar').css('width', count * 2/wordsSum * 100); 
    $('#percentage').text(Math.floor(count/wordsSum * 100) + ' %'); 
    $('#score').text(consec); 
    $('#pass').hide(); 
    $('#submit').show(); 
    $('#next').hide(); 
    $('#result').text(''); 
    $('#value').val(''); 
    randomNumber = Math.floor((Math.random() * words.length)); 
    $('#englishWord').text(words[randomNumber][0]); 
    $('#hint').click(function(){ 
     $('#value').val(words[randomNumber][1]); 
    }) 
    } 
} 

    $('#again').click(function(){ 
     location.reload(); 
    }) 

    nextQuestion(); 



    $('#value').keypress(function(e){ 
     if(e.keyCode==13) 
     $('#submit').click(); 
    }); 


}); 

내가 볼 수있는 경고로 배열을 둬서. 그들은 분명히 배열에서 제거되고 있지만 내가 가끔은 제거되었습니다 배열에서 항목을 선택하는 이유를 모르겠어요, 누군가 도와 줄 수 있습니까? 감사합니다

+0

왜 "단어 번역 단어"쌍마다 키를 추가하지 않으시겠습니까? 따라서 쉽게 삭제할 수 있습니다 – KmasterYC

+0

게임에서 항목을 연결하면 배열에서 항목을 제거합니다. 그것은 10 중 9 번 작동하는 것으로 보인다지만, 게임을 통해 그것을 이미 적어도 한 번 이상 삭제 된 배열에서 항목을 선택할 것 같습니다, 그래서 처음에 어떻게 가져 오는지 모르겠습니다. –

+0

마침내 문제가 발견되었습니다. 코드에 문제가 없습니다. 단순히 배열에 쉼표가 없습니다! –

답변

0

당신은 ',' , "estoy bien"].이게 내가 무엇일까? 너의 버그 야?

+0

예이 순간을 보았습니다. 문제가 해결되었습니다. 감사합니다. –