JavaScript更新全局变量

JavaScript updating a global variable

本文关键字:全局变量 更新 JavaScript      更新时间:2023-09-26

我目前在Javascript方面不是最有经验的,我正在努力一点一点地学习。无论如何如何更有效地更新余额变量?

目前我认为我做错了。此外,我的按钮在点击事件中不起作用。

任何事情都将是巨大的帮助!非常感谢。

// Set global variables
var name;
var balance;
var weed;
// Ask the user his name for his character
name = window.prompt("What is your name?", "Cap'n Grow");
var finalName = document.getElementById('name');
finalName.textContent = name;

// Set the balance to default
balance = 100;
var FinalBalance = document.getElementById('balance');
FinalBalance.textContent = balance;
// Set the balance of weed to default 
weed = 10;
var FinalWeed = document.getElementById('gear');
FinalWeed.textContent = weed;
// Sell function
function sellGear() {
    var check = window.prompt("Are you sure you want to sell 5 bags?", "Yes");
    if (check === 'Yes' && weed >= 5) {
        console.log("Transaction was successful!");
        // Update the balance
        var updBalance = document.getElementById('balance');
        updBalance.textContent = balance + 150;
    } else {
        console.log("Failed!")
    }
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <title></title>
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <div id="container">
            <header>
                <div class="dashboard">
                    <div id="name"></div>
                    <div id="balance"></div>
                    <div id="gear"></div>
                    <div id="sell">
                        <button id="sellButton" onlick="sellGear()">Sell?</button>
                    </div>
                </div>
            </header>
        </div>
    </body>
    <script src="js/global.js"></script>
</html>

以下是解决方案和建议:尝试在HTML的末尾使用java脚本代码。

<html lang="en">
    <head>
        <title></title>
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <div id="container">
            <header>
                <div class="dashboard">
                    <div id="name"></div>
                    <div id="balance"></div>
                    <div id="gear"></div>
                    <div id="sell">
                        <button id="sellButton" onclick="return sellGear();">Sell?</button>
                    </div>
                </div>
            </header>
        </div>
    </body>
    <script src="js/global.js"></script>
</html>
<SCRIPT>
// Set global variables
var name;
var balance;
var weed;
// Ask the user his name for his character
var name = window.prompt("What is your name?", "Cap'n Grow");
var finalName = document.getElementById('name');
finalName.textContent = name;
// Set the balance to default
var balance = 100;
var FinalBalance = document.getElementById('balance');
FinalBalance.textContent = balance;
var weed = 10;
var FinalWeed = document.getElementById('gear');
FinalWeed.textContent = weed;
// Sell function
function sellGear() {
  var check = window.prompt("Are you sure you want to sell 5 bags?", "Yes");
    if (check === 'Yes' && weed >= 5) {
        console.log("Transaction was successful!");
        // Update the balance
        var updBalance = document.getElementById('balance');
        updBalance.textContent = balance + 150;
    } else {
        console.log("Failed!")
    }
}
</SCRIPT>