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

Tell us what’s happening:

i not understand this question login anyone help me to understand

Step 96

Once a player has the best weapon, they cannot buy another one. 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.

### Your code so far

function buyWeapon() {
  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.";
  }
}

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

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

Wrap your whole function into an if statement

if (put condition here) {
//your buyWeapon() function
}

The “condition” checks if the player has less than three weapons.

1 Like

i wrap this but still got error

if(currentWeapon ← 1){
function buyWeapon() {
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.”;
}
}
}

1 Like

Check if it’s less than 3.

1 Like

done i got it thanks dude

Answer:
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.”
}
}
}

4 Likes

The worst is when you misspell something. So, when you got everything right from the answers, but your code still isn’t working, check if you didn’t misspell something or forgot to add semicolon or a bracket, or dot at the end of the sentence.

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