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/121.0.0.0 Safari/537.36
Challenge Information:
Learn Basic JavaScript by Building a Role Playing Game - Step 154
you can simply add the decrement operation (currentWeapon-- ) inside your existing if statement. Here’s how you can modify your code:
function attack() {
text.innerText = “The " + monsters[fighting].name + " attacks.”;
text.innerText += " You attack it with your " + weapons[currentWeapon].name + “.”;
health -= getMonsterAttackValue(monsters[fighting].level);
// Decrement currentWeapon if it’s greater than 0
if (currentWeapon > 0) {
currentWeapon–; // Decrementing currentWeapon
}
I think the decrement operator on the currentWeapon variable needs to go with the if statement, when if true prints Your … weapon breaks to the screen.
I struggled with Step 154 as well, because i felt the instructions were a bit unclear. But the solution was actually quite simple: In your current if Statement (the one that makes the weapon randomly break) just make sure to add a statement to the codeblock, that decreases the value of currentWeapon by one. So for anyone still looking for a solution, the above code worked for me.
Challenge Information:
Learn Basic JavaScript by Building a Role Playing Game - Step 154
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
Hey was also stuck on number 154 for a while but finally got it
Actually quiet simple: just add decrement of currentWeapon here:
if (Math.random() <= .1 ) {
text.innerText += " Your " + inventory.pop() + " breaks.";
currentWeapon- -;
}
Happy Coding hang in there:+1:t5: