2015-05-13 69 views
0

我對java腳本很新,所以請耐心等待:)所以我在java腳本(匹配動物打印與他們的名字)中進行匹配遊戲,即使我點擊正確的答案,分數永遠不會被添加,因此你總是輸。在匹配的遊戲java腳本中添加分數

我該如何解決這個問題?

這裏是我的代碼

function randSort (a,b) { 
 
    return Math.random() - 0.5; 
 
} 
 

 
var questions = [ 
 
    { 
 
    text: " What animal is this?", 
 
    img: "AnimalPrints/1.jpg", 
 
    answers: ["Cheetah", "Tiger", "Ladybird"], 
 
    ans: "A" 
 
    }, 
 
    { 
 
    text: " What animal is this one?", 
 
    img: "AnimalPrints/2.jpg", 
 
    answers: ["Elephant", "Giraffe", "Snake"], 
 
    ans: "B" 
 
    }, 
 
    { 
 
    text: "What animal is this one please?", 
 
    img: "AnimalPrints/3.jpg", 
 
    answers: ["Bumblebee", "Tiger", "Lady bird"], 
 
    ans: "Bumblebee" 
 
    } 
 
]; 
 

 
var correctCount = 0; 
 
var currentQ = 0; 
 

 

 
function select(nr) { 
 
    if (nr == questions[currentQ].ans) 
 
    { 
 
    correctCount++; 
 
    document.getElementById('display').innerHTML= "You win" 
 
    } 
 
    else 
 
    { 
 
    document.getElementById('display').innerHTML= "You lose" 
 
    } 
 
    document.getElementById('display').innerHTML += "<p>Score: "+ correctCount; 
 

 
    // if its the last one 
 
    nextQ(); 
 

 
} 
 

 
function showQ() { 
 
    document.getElementById('questionText').innerHTML = questions[currentQ].text; 
 
    document.getElementById('animalPrint').src = questions[currentQ].img; 
 
    newhtml = ""; 
 
    for (var i = 0; i< questions[currentQ].answers.length; i++) 
 
    { 
 
    newhtml+= "<button onclick = 'select(" + i + ")'>"+ questions[currentQ].answers[i] + "</button>"; 
 
    } 
 
    document.getElementById('alloptions').innerHTML = newhtml; 
 
} 
 

 

 
function nextQ(){ 
 
    if (currentQ < questions.length-1) 
 
    { 
 
    currentQ++; 
 
    showQ(); 
 
    } 
 
} 
 

 

 
window.onload =init; 
 

 

 
function init() 
 
{ 
 
    correctCount = 0; 
 
    questions.sort(randSort); 
 
    currentQ = 0; 
 
    showQ(); 
 
}
body { 
 
    background-position: center; 
 
    background-color:lime; 
 
} 
 

 
#questionText {width: 350px; background: white;} 
 

 

 
#nextbutton { 
 
    background-image: url(Buttons/nextbutton.jpg); 
 
    background-size:contain; 
 
    background-repeat:repeat-y; 
 
    background-position: center; 
 
    width:100px; 
 
    height:44px; 
 
    margin-left: 250px; 
 
    border-radius:10px; 
 
} 
 

 

 
#main { 
 
    margin-top:200px; 
 
    margin-left:250px; 
 
    border:1px solid red; 
 
    width:600px; 
 

 
} 
 

 
#display { 
 
    width:150px; 
 
    height:50px; 
 
    background-color:blue; 
 
    color:white; 
 
    border-radius:5px; 
 
    font-family:aqua; 
 
}
<div id = main> 
 
    <div id = "questionText"> Testing</div> 
 
    <div id ="animal"> 
 
    <img id = "animalPrint" src = "AnimalPrints/1.jpg"> 
 
    </div> 
 

 
    <div id = "alloptions"> </div> 
 

 
    <button id = "nextbutton" onclick = "nextQ();"></button> 
 
</div> 
 
<div id = "display"> Score: </div>

+2

[如何創建一個最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) – melancia

回答

0

您傳遞(數字,0爲主)的答案指數基於你單擊該按鈕的功能select。您將其與ans財產進行比較,該財產的第一個問題值爲'A''B'

0,顯然不等於'A'。

因此,將ans更改爲0到N的值,指定正確答案的索引,它起作用。

+0

我似乎沒有把它放在我的代碼的正確部分,對不起,我是仍然在學習......你能準確地說出我把它放在哪裏嗎? – MysteryX

+0

是的。將'ans:「A」'更改爲'ans:0'。 'ans:「B」到'ans:1'和'ans:「Bumbelbee」'到'ans:0'。 – GolezTrol

+0

非常感謝!已經排序! – MysteryX