Hi everyone! I don't really understand what am to do in javaScript step 95. Please can anyone guide or give me more explanation here?

Change your if condition to check if currentWeapon is less than the length of the weapons array. An example of checking the length of an array myArray would look like myArray.length.

Hi! It’s impossible to help you without your code, so share it first, please

here is my code below:

 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;
      weapon.length
    } else {
      text.innerText = "You do not have enough gold to buy a weapon.";
    }
  }
}

Well, you’re very close.
Note, you have to get length of the weapons (plural) array and put it into your if condition to compare the currentWeapon with the length of the weapons array.
if (currentWeapon < ...)

1 Like

My code is yet to pass.

my code below:


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

The length of the weapons array, fix it and your code will pass

I don’t know where to fix it in my code

In this line of code

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