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

Tell us what’s happening:

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.

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

kindly share your code for getting more information to solve it

function attack() {
  text.innerText = "The " + monsters[fighting].name + " attacks.";
  text.innerText += " You attack it with your " + weapons[currentWeapon].name + ".";
  health -= getMonsterAttackValue(monsters[fighting].level);
  if (isMonsterHit()) {
    currentWeapon--;
    monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1;
  } else {
    text.innerText += " You miss.";
  }
  healthText.innerText = health;
  monsterHealthText.innerText = monsterHealth;
  if (health <= 0) {
    lose();
  } else if (monsterHealth <= 0) {
    if (fighting === 2) {
      winGame();
    } else {
      defeatMonster();
    }
  }
  if (Math.random() <= .1) {
    text.innerText += " Your " + inventory.pop() + " breaks.";

  }
}

help to solve the issues

function attack() {
  text.innerText = "The " + monsters[fighting].name + " attacks.";
  text.innerText += " You attack it with your " + weapons[currentWeapon].name + ".";
  currentWeapon--;
  health -= getMonsterAttackValue(monsters[fighting].level);
  if (isMonsterHit()) {
    monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1;
  } else {
    text.innerText += " You miss.";
  }
  healthText.innerText = health;
  monsterHealthText.innerText = monsterHealth;
  if (health <= 0) {
    lose();
  } else if (monsterHealth <= 0) {
    if (fighting === 2) {
      winGame();
    } else {
      defeatMonster();
    }
  }
  if (Math.random() <= .1) {
    text.innerText += " Your " + inventory.pop() + " breaks.";

  }
}

also tell me your problem it is not showing in post

You should decrement currentWeapon in your if statement .

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
}

if (isMonsterHit()) {
monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1;
} else {
text.innerText += " You miss.";
}
healthText.innerText = health;
monsterHealthText.innerText = monsterHealth;

if (health <= 0) {
lose();
} else if (monsterHealth <= 0) {
if (fighting === 2) {
winGame();
} else {
defeatMonster();
}
}

if (Math.random() <= .1) {
text.innerText += " Your " + inventory.pop() + " breaks.";
}
}
check this code may be it will work for you

no its not can you help to solve the issue

function attack() {
  text.innerText = "The " + monsters[fighting].name + " attacks.";
  text.innerText += " You attack it with your " + weapons[currentWeapon].name + ".";
  health -= getMonsterAttackValue(monsters[fighting].level);
  
  // Check if currentWeapon is greater than 0 before decrementing
  if (currentWeapon > 0) {
    currentWeapon--; // Decrement currentWeapon
  }

  if (isMonsterHit()) {
    monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1;
  } else {
    text.innerText += " You miss.";
  }
  healthText.innerText = health;
  monsterHealthText.innerText = monsterHealth;
  
  if (health <= 0) {
    lose();
  } else if (monsterHealth <= 0) {
    if (fighting === 2) {
      winGame();
    } else {
      defeatMonster();
    }
  }
  
  if (Math.random() <= .1) {
    text.innerText += " Your " + inventory.pop() + " breaks.";
  }
}

This way, currentWeapon will only be decremented if it’s greater than 0, preventing it from becoming negative.

check this

Hi @kishor07

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.

Happy coding

1 Like

solution removed by moderator

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

The correct solution is to just add removed after your last line of code

SOLUTION REMOVED BY MOD

Hi @sierraghankins and @MacDev1 !

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.

You need to add just decrement '– sign to your currentWeapon` .

Hey was also stuck on number 154 for a while but finally got it :sunglasses::sunglasses:
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::+1:t5::+1:t5::+1:t5:

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