簡體   English   中英

從數組中刪除項目,但項目似乎返回

[英]Removing item from array but item seems to return

我正在創建一個翻譯游戲,它將從數組中隨機選擇一個單詞,如果用戶選擇了正確的答案或通過,則將從數組中刪除該單詞。 似乎在播放此文字時,它將隨機選擇一個先前已刪除的單詞,並且由於已刪除了該單詞,因此沒有任何內容可以檢查用戶的回答。

這是小提琴,您可以使用提示按鈕播放並用正確的答案進行測試。

這是添加到js小提琴網址末尾的鏈接-#&togetherjs = U713xeqrK3“

這是JavaScript。

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


});

通過將陣列置於警報中,我可以看到它們顯然已從陣列中移除,但是我不知道為什么它偶爾會從已移除的陣列中選擇一個項目,有人可以幫忙嗎? 謝謝

您在[“我很好”,“ estoy bien”]之后缺少','。 可能這是導致您的錯誤的原因嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM