Hi i believe i am on the right track but stuck on what text to add in
function buyHealth() {
gold -= 10;
health += 10;
goldText.innerText = gold;
healthText.innerText = health;
if (gold >= 10) {
console.log()
}
}
Hi i believe i am on the right track but stuck on what text to add in
function buyHealth() {
gold -= 10;
health += 10;
goldText.innerText = gold;
healthText.innerText = health;
if (gold >= 10) {
console.log()
}
}
“Start by placing all of the code in your buyHealth
function inside an if
statement.”
Instead of placing all of the code inside of an if
statement, you added the if
statement after the code. There are four lines of code in the buyHealth
function. You want to place all of those lines inside one if
statement. And forget about the console.log
. That was just in the example as something to go inside the if
statement. It is not needed for this step.
function buyHealth() {
const gold = 10;
if (gold >= 10);
}
gold -= 10;
health += 10;
goldText.innerText = gold;
healthText.innerText = health;
}
You are halfway there.
Delete this line as the gold
variable you created globally at the top start of your code file
(line number 3).
and the starting curly bracket of your if
statement is somehow reversed and you don’t need a semicolon ;
after your condition.
the semicolon is used to end a statement and your whole if
with its curly brackets is considered a statement,
so you can use that semicolon after your closing curly bracket not after your condition parentheses.
Example:
if (condition) {
// code
};
which curly bracket are you talking about is reversed?
function buyHealth() {
if (gold >= 10)
};
gold -= 10;
health += 10;
goldText.innerText = gold;
healthText.innerText = health;
}
That one next to the ;
and then i explained why there shouldn’t be ;
there.
function buyHealth() {
if (gold >= 10)
gold -= 10;
health += 10;
goldText.innerText = gold;
healthText.innerText = health;
};```
so i can add the colon at the end of the equation?
Yep you can, but you missed the opening curly bracket of your if
statement.
Also you missed the closing curly bracket for your function.
it should be like this:
function buyHealth() {
if (gold >= 10) {
gold -= 10;
health += 10;
goldText.innerText = gold;
healthText.innerText = health;
};
}
Just like this template:
if (condition) {
// your code
};
But in general, don’t use the semicolon after your curly brackets.
cause that’s what the curly brackets do for you in advance (it determines the beginning and the end of your block of code).
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.