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

how can I put my existing IF to my new IF here in this code

function buyWeapon() {
// Check if the player has enough gold to buy a weapon
if (gold < 30) {
text.innerText = “You do not have enough gold to buy a weapon.”;
} else {
// Check if the player already has the most powerful weapon
if (currentWeapon < 3) {
gold -= 30;
currentWeapon++;
goldText.innerText = gold;
text.innerText = "You now have a " + weapons[currentWeapon].name + “.”;
inventory.push(weapons[currentWeapon].name);
text.innerText += " In your inventory you have: " + inventory;
} else {
// Player already has the most powerful weapon
text.innerText = “You already have the most powerful weapon!”;
button2.innerText = “Sell weapon for 15 gold”;
button2.onclick = sellWeapon;
}
}
}

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 all of the code in your buyWeapon function inside another if statement.”

This means don’t make any changes to the existing code. You only want to wrap all of the existing code inside of the buyWeapon function in a new if statement.

still stuck bro

HELP

my console says this

Your existing if statement should be within your new if statement.

wraped with a new if statement and my console says this
(Your existing else statement should be within your new if statement.)

You’ll need to show us your updated code so we can see what you are doing. Please use the following method to paste your code in here so we can read it properly.

To display your code in here you need to wrap it in triple back ticks. On a line by itself type three back ticks. Then on the first line below the three back ticks paste in your code. Then below your code on a new line type three more back ticks. The back tick on my keyboard is in the upper left just above the Tab key and below the Esc key. You may also be able to use Ctrl+e to automatically give you the triple back ticks while you are typing in the this editor and the cursor is on a line by itself. Alternatively, with the cursor on a line by itself, you can use the </> button above the editor to add the triple back ticks.

One suggestion I might make is to reset the step to get the original code back. Then, do not make any changes to code that is already there. You only want to wrap all of the existing code in the buyWeapon function in a new if statement.

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

1 Like