简体   繁体   中英

Removing item from array but item seems to return

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"

Here is the 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM