Reputation: 23
I need it to show up as "0" before you click, and for some reason, it shows up as NaN, help!
<html>
<head>
<script>
var money = 0;
var clickingPower = 10;
function moneyClick() {
var clicks = clicks + 1;
document.getElementById("money").innerHTML = money + clickingPower * clicks;
}
</script>
</head>
<h1>You have <span id="money"></span> dollars</h1>
<button onclick="moneyClick()">Click to make money</button>
</body>
</html>
Upvotes: 1
Views: 113
Reputation: 36
<script>
var money = 0;
var clicks = 0;
var clickingPower = 10;
function moneyClick() {
clicks = clicks + 1;
document.getElementById("money").innerHTML = money + clickingPower * clicks;
}
</script>
Upvotes: 0
Reputation: 11
You need to initialize your variable. Make sure you put this outside of your function. Var clicks = 0
Upvotes: 1
Reputation: 434
You're using clicks
before defining it.
you can try this:
<html>
<head>
<script>
var money = 0;
var clickingPower = 10;
function moneyClick() {
var clicks = (clicks || 0) + 1;
document.getElementById("money").innerHTML = money + clickingPower * clicks;
}
</script>
</head>
<h1>You have <span id="money"></span> dollars</h1>
<button onclick="moneyClick()">Click to make money</button>
</body>
</html>
Upvotes: 2
Reputation: 129
You have to initialize the variable clicks
:
var clicks = 0;
function moneyClick() {
clicks = clicks + 1;
document.getElementById("money").innerHTML = money + clickingPower * clicks;
}
Upvotes: 1
Reputation: 2376
It this line
var clicks = clicks + 1
Your variable clicks does not exist yet, that is why you get NaN
Upvotes: 3