javascript - I need help writing this IF statement in JS -
i have game enter score input box each turn take. wrote loop add total of each turn , have displayed in "total score" box. in game, if "total score" @ end of of turns greater or equal 50, 10 point bonus.
so i've created input box called "total". want create if statement if "total score" input box has number in greater or equal 50, score plus 10 point bonus displayed in "total" box beneath it. how can this?
here 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; (var = 0; < arr.length; i++) { if (parseint(arr[i].value)) tot += parseint(arr[i].value); } document.getelementbyid('total').value = tot; } onblur = "findtotal()" //my attempt @ if statement ("alert hello" place holder) var tots = document.getelementbyid('totalscore'); if (tots.value >= 50) { alert("hello"); }
you try checking see if user got bonus @ end of findtotal
function (where anyways finished calculating total score
):
function findtotal() { var arr = document.getelementsbyname('qty'); var tot = 0; (var = 0; < arr.length; i++) { if (parseint(arr[i].value)) tot += parseint(arr[i].value); } document.getelementbyid('totalscore').value = tot; var bonus = tot >= 50 ? 10 : ''; document.getelementbyid('bonus').value = bonus; document.getelementbyid('total').value = tot + bonus; }
turn 1: <input onblur="findtotal()" type="text" name="qty"> <br> turn 2: <input onblur="findtotal()" type="text" name="qty"> <br> turn 3: <input onblur="findtotal()" type="text" name="qty"> <br> <b>total score: </b> <input disabled="true" type="text" name="totalscore" id="totalscore"> <br> bonus: <input disabled="true" type="text" id="bonus"> <br> <b>total:</b> <input disabled="true" type="text" id="total">
Comments
Post a Comment