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

Tell us what’s happening:

The value of the currentWeapon variable corresponds to an index in the weapons array. The player starts with a stick, since currentWeapon starts at 0 and weapons[0] is the stick weapon.

In the buyWeapon function, use compound assignment to add 1 to currentWeapon - the user is buying the next weapon in the weapons array.

I’m not sure what I’m doing wrong and I’ve tried multiple different solutions.

Your code so far

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.

function buyWeapon() {
  if (gold >= 30) {
    gold -= 30;
  };
  if (currentWeapon = 0) {
    currentWeapon += 1;
  }
}

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 85

You use an if to check if currentWeapon is 0.

Why does it matter if currentWeapon is 0? What if currentWeapon is 2?

Comparison operator is ==

Instructions also don’t really mention an if statement, best to follow them exactly.

it’s supposed to be in an if statement

you already have if statement created , so just use compound assignment ++ after currentWeapon to use next weapon. I hope this will help!

In the buyWeapon function, use compound assignment to add 1 to currentWeapon - the user is buying the next weapon in the weapons array.

Doesn’t mention an if statement. You do already have an if statement you can add it to. I would structure it just like the buyHealth function.

Just add “currentWeapon += 1” in If statement

1 Like

this has been tried and it does not work

the instructions don’t mention an if statement but if I submit the answer without the if statement it says there needs to be an if statement

i’ve tried this, it doesn’t work

Don’t remove the if statement that’s already there and don’t add a new one.

All you need to do is add a line that does this:

use compound assignment to add 1 to currentWeapon

And nothing else

Please share your updated code.

hello not sure if you figured it out but currentWeapon += 1; after the gold worked for me

i also tried but it didnt … </3

This needs to be in the if statement:

currentWeapon += 1;

function buyWeapon() {
if (gold >= 30) {
gold -= 30;
currentWeapon +=1;
}
}