从数组中删除项目,但项目似乎返回

时间:2016-08-18 17:55:36

标签: javascript jquery html arrays splice

我正在创建一个翻译游戏,它会从数组中随机选择一个单词,如果用户选择正确的答案或传递该单词将从数组中删除。在玩这个游戏时,它会随机选择一个之前被移除过的单词,因为它已被删除,所以没有任何内容可以检查用户在删除时的回答。

这是小提琴,您可以使用提示按钮播放并使用正确的答案进行测试。

这是添加到js fiddles url末尾的链接 - #& 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();
    });


});

通过将数组放入警报我可以看到他们显然已从数组中删除但我不知道为什么它偶尔会从数组中删除一个已被删除的项目,有人可以帮忙吗?感谢

1 个答案:

答案 0 :(得分:0)

在[“我很好”,“estoy bien”]之后你错过了','。这可能是导致你的错误的原因吗?