【问题标题】:Removing item from array but item seems to return从数组中删除项目但项目似乎返回
【发布时间】:2016-12-25 18:22:23
【问题描述】:

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

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

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


});

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

【问题讨论】:

  • 为什么不为每个“单词-翻译单词”对添加一个键?所以你可以轻松删除它
  • 当在游戏中拼接一个项目时,它会从数组中移除它。它似乎在 10 次中有 9 次有效,但似乎当你运行游戏时,它会从数组中选择一个已经被删除至少一次的项目,所以我不确定它是如何被选中的。
  • 终于找到问题了,代码本身没有问题,只是数组中少了一个逗号!

标签: javascript jquery html arrays splice


【解决方案1】:

在 ["I am fine", "estoy bien"] 之后缺少一个 ','。这可能是导致您的错误的原因吗?

【讨论】:

  • 是的,我刚刚看到这个并且已经解决了这个问题!谢谢
猜你喜欢
  • 1970-01-01
  • 2021-10-19
  • 2018-06-02
  • 2021-10-03
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
相关资源
最近更新 更多