mardi 25 juillet 2017

I need help writing this IF statement in JS

I have a game where you enter your score into an input box for each turn you take. I wrote a for loop to add the total of each turn and have it displayed in the "Total Score" box. However in this game, if your "Total Score" at the end of all of your turns is greater than or equal to 50, you get a 10 point bonus.

So I've created another input box called "Total". I want to create an IF statement so that if the "Total Score" input box has a number in it that is greater than or equal to 50, your score plus the 10 point bonus is displayed in the "Total" box beneath it. How can I do this?

Here is my code:

//input form   

Turn 1: <input onblur="findTotal()" type="text" name="qty">
<br>
Turn 2: <input onblur="findTotal()" type="text" name="qty">
<br>
Trun 3: <input onblur="findTotal()" type="text" name="qty">
<br>
<b>Total Score:</b> <input type="text" name="TotalScore" id="TotalScore">
<br>
Bonus (+10): <input type="text" name="bonus">
<br>
<b>Total:</b> <input type="text" name="Total">

//for loop

function findTotal() {
    var arr = document.getElementsByName('qty');
    var tot = 0;
    for (var i = 0; i < arr.length; i++) {
        if (parseInt(arr[i].value))
            tot += parseInt(arr[i].value);
    }
    document.getElementById('Total').value = tot;
}
onblur = "findTotal()"

//my attempt at the IF statement ("alert hello" is just a place holder)

var tots = document.getElementById('TotalScore');

if (tots.value >= 50) {
    alert("hello");
}

Aucun commentaire:

Enregistrer un commentaire