You have the right idea, but there were a few issues with your code.
Side Note: In programming, it’s pretty much always a good idea to name your variables/functions something meaningful and descriptive.
In the code sample below, I changed the name of your function hey
to increment
since the purpose of that function is to increment the value of our variable (which I also renamed i
).
I also replaced your document.write()
with a console.log()
//create a global variable called i
var i = 0
//increment i each time the function is called
function increment() {
i = ++i;
console.log(i);
}
<!DOCTYPE html>
<html>
<body>
<button onclick="increment()">Increment</button>
<script>
</script>
</body>
</html>
1
solved Button to increment a number on screen