JavaScript updating a global variable

I am not the most proficient in Javascript at this time and I am trying to learn in half. Anyway ... how can I update the balance variable more efficiently?

I currently believe I am doing it wrong. Also my button doesn't work on click event.

Anything that would be a huge help! Thank.

// 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>

      

+3


source to share


1 answer


Here is the solution and suggestion: Try using the java script code at the end of your 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" 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>

      

+1


source







All Articles