Learn Basic JavaScript by Building a Role Playing Game - Step 96

Please i’m stuck on line 96.
Wrap all of the code in your buyWeapon function inside another if statement. The condition should check if currentWeapon is less than 3 - the index of the last weapon.

function buyWeapon() {
if (currentWeapon < 3) {
gold -= 30;
currentWeapon++;
goldText.innerText = gold;
let newWeapon = weapons[currentWeapon].name;
text.innerText = "You now have a " + newWeapon + “.”;
inventory.push(newWeapon);
text.innerText += " In your inventory you have: " + inventory;
if (gold >= 30) {
gold -= 30;
currentWeapon++;
goldText.innerText = gold;
let newWeapon = weapons[currentWeapon].name;
text.innerText = "You now have a " + newWeapon + “.”;
inventory.push(newWeapon);
text.innerText += " In your inventory you have: " + inventory;
} else {
text.innerText = “You do not have enough gold to buy a weapon.”;
}

}
Please help. it didnt pass

I guess you need to check at the same time that the gold is higher than 30 and currentWeapon is less than 3, because you can’t do gold -= 30 if that results in a value of gold that is negative

were you ever able to figure this part out? I have been stuck on it for a while. It does say in the instruction you have to check that currentWeapon is 3 less than the index of the last weapon. When you fail, it just says to make sure currentWeapon is less than 3. But maybe there is something to distinguish 3 less than the the index of the last weapon, but I’m not sure how to do that or if it’s correct. Just the only idea I have currently :clown_face:

ok somehow magically I just got this to pass. After trying a million things, it gave me a hint that my ending curly bracket for the new if statement needed to be included after the else statement. I think you are just missing your 3rd end bracket?

"
function buyWeapon() { if(currentWeapon < 3) {
if (gold >= 30) {
gold -= 30;
currentWeapon++;
goldText.innerText = gold;
let newWeapon = weapons[currentWeapon].name;
text.innerText = "You now have a " + newWeapon + “.”;
inventory.push(newWeapon);
text.innerText += " In your inventory you have: " + inventory;
} else {
text.innerText = “You do not have enough gold to buy a weapon.”;
}
}
}
"

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.