mercredi 8 juillet 2015

Javascript if statement doesn't work as expected

I'm trying to create a small survey with a score. I want to make it so once user reaches score "4", it alerts some message. I wrote if statment for it, but for some reason it never alerts. But if I manually change score variable to 4, everything works as expected.

Where am I going wrong?

Here's the code:

http://ift.tt/1eEYkHD

<script>
var score = 0;

function checkQuestionOne (){

    var jautViens = document.getElementById('jaut1Pirm');
    var jautDivi = document.getElementById('Jaut1Otra');
    var jautTris = document.getElementById('Jaut1Tres');
    var jautCetri = document.getElementById('Jaut1Cetu');

    if(jautViens.checked) {
        alert(++score)
    }else if(jautDivi.checked || jautTris.checked || jautCetri.checked) {
        alert("Nepareizi")
    }else{
        alert("Izvēlies opciju!")
    }
}

if (score == 4){
    alert(score);

}else{

}

Aucun commentaire:

Enregistrer un commentaire