Reputation: 2260
I'm building little game which will also have stats. Win times and things like that. So I made this html
<div id="stats">
<h2>Stats</h2>
<p>Win ratio: </p> <!-- Ignore this one -->
<p><span id="wincount"></span> wins / <span id="lostcount"></span> losses</p>
</div>
And here is the Javascript code. I little bit shorted it so you can see only important things:
var wontimes = 0;
var losstime = 0;
function getResult(){
var tie = "It's a tie!";
var won = "You have won!";
var lost = "You lost!";
switch(user){
case 1: // rock
switch(npc){
case 1: // rock
result = tie;
break;
case 2: // paper
result = lost;
break;
}
break;
case 2: // paper
switch(npc){
case 1: // rock
result = won;
break;
case 2: // paper
result = tie;
break;
}
break;
}
document.getElementById("gameresult").innerHTML = result;
if (result === won){
return wontimes = wontimes++;
} else if (result === lost){
return losstime = losstime++;
} else {
console.log("Tie");
}
document.getElementById("wincount").innerHTML = wontimes;
document.getElementById("lostcount").innerHTML = losstime;
}
Function getResult is called when user clicks button and it works, but there is a problem with it. After I click it. It tells me if I won and etc. But it doesn't count winning. I wanted to make it that everytime user win, wontimes get one plus and then it gets printed in <span id="wincount"></span>
, it doesn't even show default 0 wins.
Can someone help me? Thank you.
Upvotes: 0
Views: 998
Reputation: 382274
If you return from the function, the lines after are ignored.
Change
return wontimes = wontimes++;
to
wontimes++;
(and the same for losstime
)
Note also that wontimes++
is enough for incrementing a variable. If you do wontimes = wontimes++
, you give to wontimes
the value it had before the incrementing, which makes the whole useless.
Upvotes: 6