[Solved] Why wont this code show the hunger variable? [closed]


You need to have a div with the id. This code increments the value. Also, you don’t need both document.write and setting the innerHTML.

Code:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://stackoverflow.com/questions/66876926/styles/style.css"/>
    <title>Hunger!</title>
</head>
<body>
    <div id="hunger-div"></div>
    <script>
        var health = 5;
        var hunger = 10;
        function task(i) {
            setTimeout(function() {
                hunger -= 1;
                document.getElementById('hunger-div').innerHTML = `<b>${hunger}</b>`;
            } , 10000 * i);
        }   
    
        for (let i = 0; i<10; i++) {
            task(i);
        }
    </script>
</body>
</html>

6

solved Why wont this code show the hunger variable? [closed]