javascript - Changing global Variable from within function -
here im having bit of issue simple script ive written up. aim script reduce given number 1 each time button clicked. cannot appear this.. global variable being number=100 doesnt appear change, or change more once.. apologies not being able explain well. here part im working on..:
<script> var number = 100; // number want changed , keep changing each button click function outcome() { // button calls function number = number - 1; // tries change global number.. :/ } document.write(number); // has number written in document </script>
yes, conceptually right. not calling function, @ least not before writing number
document.
btw, number
global reference number
constructor should use variable name, lowercase @ best.
var num = 100; function outcome() { num--; } outcome(); document.write(num); // 99
or
<script> var num = 100; function outcome() { num--; alert(num); } </script> <button onclick="outcome()">decrease!</button>
Comments
Post a Comment