2016-08-18 34 views
0

翻訳ゲームを作成していますが、配列からランダムに単語を選択し、ユーザーが正しい答えを選択するか、単語が配列から削除されます。これをプレイすると、以前に削除された単語がランダムに選択され、削除されたため、削除されたユーザーの回答をチェックすることはありません。配列から項目を削除しても項目が返されているようです

ここではフィドルですが、ヒントボタンを使用して正しい答えで再生してテストすることができます。ここで

はJSフィドルのURLの末尾に追加するためのリンクがある - 。ここで

はjavascriptのある#& togetherjs = U713xeqrK3"

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


}); 

私は見ることができ、アラートに配列を入れて彼らは明らかに配列から削除されていますが、私は時折それが削除されている配列から項目を選択して、誰かを助けることができるのか分からない?ありがとう

+0

「単語翻訳された単語」のペアごとにキーを追加してみませんか?あなたはそれを簡単に削除することができます – KmasterYC

+0

アイテムをゲームにスプライスすると、それが配列から削除されます。それは10回のうち9回働いているようですが、あなたがゲームを走ったときのように、少なくとも1回は既に削除されている配列から項目を選択するように思われるので、どのようにこれが最初に選ばれるのか分かりません。 –

+0

最後に問題が見つかりました。コードに問題はありませんでした。配列にカンマがありません。 –

答えて

0

あなたは[ 、 "estoy bien"]これは私があなたのバグの原因は何ですか?

+0

はい、私は今分だけこれを見て、問題を解決しました! ありがとう –

関連する問題