javascript - Arithmetic with variables not working -
for reason, arithmetic in function updatescore(result)
doesn't work (the function called later on in code). wins
, ties
, losses
printed should, lives
printed nan. know nan means. i've identified variables, reason or other, created strings. appears strange me, it's working 4 out of 5 variables. there's no consistency. i've tried number conversions using number(lives)
, doesn't work either. suggestions how can ensure variables created numbers, , aritmethic operations work?
var wins = 0, ties = 0, losses = 0, lives = 5, previouscpuchoice = 0; $("#startknapp").click(function(){ var spiller = prompt("hva heter du?"); $("#userselect").html(usermenu); $("#result").html("<h4>velg figur, " + spiller + "</h4>"); $("#status").html('<h4>liv: <span id="life">' +lives+ '</span> - seire: <span id="win">' + wins + '</span> - uavgjort: <span id="tie">' + ties + '</span> - tap: <span id="lose">' + losses + '</span>'); console.log(typeof "lives"); console.log(typeof "wins"); }); function updatescore(result) { tie = document.getelementbyid("tie"); win = document.getelementbyid("win"); lose = document.getelementbyid("lose"); lives = document.getelementbyid("life"); console.log(typeof "wins"); var imgsrc = "images/" + userchoice + "-" + result + ".png"; if (result === "tie") { ties = ties + 1; tie.innerhtml = ties; $('.result-img').attr('src', 'images/tie.png'); } if (result === "vant") { wins++; $('.result-img').attr('src', imgsrc); } if (result === "tapte") { losses++; lives--; lose.innerhtml = losses; life.innerhtml = lives; $('.result-img').attr('src', imgsrc); } };
have tried following?
var tie = parseint(document.getelementbyid("tie").value);
your current code:
document.getelementbyid("tie")
retrieves dom element rather value of input. need use .value
, parse integer inputs value string representation of number want rather integer value.
Comments
Post a Comment