3

I am currently not the most experienced in Javascript and I'm trying to learn bit by bit. Anyway... how do I update the balance variable more efficiently?

Currently I believe I am doing this wrong. Also my button does not work on click event.

Anything would be a massive help! Thank you.

// 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>
Ian Hazzard
  • 7,661
  • 7
  • 34
  • 60
user3274696
  • 89
  • 1
  • 7

1 Answers1

1

Here is the solution and a suggestion: Try to use 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>
PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
  • Well before in my sell function I just had balance = balance = 50; However this doesn't increment my balance when I press sell, how come? thank you – user3274696 Nov 03 '14 at 16:37
  • you were using "onlick" instead of "onclick" :) and it's my pleasure to help you.. – PHP Worm... Nov 03 '14 at 16:39