2016-08-18 45 views
0

我創建一個翻譯的遊戲,它選擇隨機一個字排列的,如果用戶選取正確的答案或經過字從數組中刪除。看起來在播放它時,它會隨機選擇一個先前已被刪除的單詞,並且由於它已被刪除,因此它沒有任何內容可以檢查用戶在刪除時的回答。移除項目似乎返回

這裏是小提琴,你可以使用提示按鈕來播放,並與正確答案進行測試。

這裏是添加到爵士小提琴網址末尾的鏈接 - #& 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(); 
    }); 


}); 

通過將數組轉換成一個警告,我可以看到。他們顯然正在從陣列中刪除,但我不知道爲什麼它偶爾從已刪除陣列的動產項目,可有人幫忙嗎?謝謝

+0

爲什麼不爲每個「單詞翻譯單詞」對添加一個鍵?因此,您可以輕鬆刪除它 – KmasterYC

+0

在遊戲中拼接某個物品時,它會將其從陣列中移除。它似乎在10次中有9次工作,但似乎當你通過遊戲運行時,它會從數組中選擇一個已經被刪除至少一次的項目,所以我不知道如何在首先被選中。 –

+0

終於發現問題了,沒有問題,代碼就這樣,簡單地在數組中丟失了一個逗號! –

回答

0

你缺少一個「」後[‘我很好’ ,「estoy bien」]。可能這就是我導致你的錯誤?

+0

是的,我剛剛看到了這一點,並解決了這個問題! 謝謝 –