how do I update my variable using a function

64 views Asked by At

I can't get the health variable updated..I get an update if i console.log it inside the function but it won't outside?

var health;
health = 9;

function scaringCat(event) {
    var catAngry = new Audio("sounds/cat_hiss.mp3");
    catAngry.play();

    health = health -1;
    return health;
}

buttons[1].addEventListener('click', scaringCat);
1

There are 1 answers

0
shreyansh On

Have a look at this simple javascript code its updating health variable every time.Check console.

<body>

            <input type="button" value="Submit1" onclick="scaringCat()"></input>
            <input type="button" value="Submit2" onclick="scaringCat1()"></input>



<script type="text/javascript">
 var health;
    health = 9;

    function scaringCat() {
    health = health -1;

    console.log(health);
     alert("click on submit2 now!!!")
}

    function scaringCat1() {
    var v=health;
    console.log(v);

    }
    </script>

</body>