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

时间:2022-06-13 17:27:13

I am creating a translation game, it picks a word at random out of the array and if the user picks the right answer or passes the word is removed from the array. It seems when playing this it will then randomly chose a word that has previously been removed and because its been removed it has nothing to check the users answer against as its removed.

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

Here is the fiddle, you can use the hint button to play and test this with the correct answer.

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

Here is the link to add to the end of js fiddles url - #&togetherjs=U713xeqrK3"

这是添加到js fiddles url末尾的链接 - #&togetherjs = U713xeqrK3“

Here is the javascript.

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


});

By putting the array into a alert i can see they are clearly being removed from the array but i have no idea why it occasionally choses an item from the array which has been remove, can somebody help? Thanks

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

1 个解决方案

#1


0  

You are missing a ',' after ["I am fine", "estoy bien"]. Might this be what is causing your bug?

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

#1


0  

You are missing a ',' after ["I am fine", "estoy bien"]. Might this be what is causing your bug?

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