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

Tell us what’s happening:

You should not modify your existing if statement.

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

function attack() {
  text.innerText = "The " + monsters[fighting].name + " attacks.";
  text.innerText += " You attack it with your " + weapons[currentWeaponIndex].name + ".";
  health -= getMonsterAttackValue(monsters[fighting].level);
  if (isMonsterHit()) {
    monsterHealth -= weapons[currentWeaponIndex].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() > 0.2 && inventory.length !== 1) {
    text.innerText += " Your " + inventory.pop() + " breaks.";
    currentWeaponIndex--;
  }
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15

Challenge Information:

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

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

Attack Messages: It first tells you that the monster is attacking and then lets you know you’re hitting back with your current weapon.
Taking Damage: Your character’s health drops depending on how powerful the monster’s attack is.
Hitting the Monster: The game checks if you land your hit. If you do, the monster takes damage based on your weapon’s power and some random boost from your experience. If you miss, the game tells you.
Updating Health: It then shows the latest health status for both you and the monster on the screen.
Winning or Losing: If your health reaches zero, you lose. If the monster’s health hits zero, you either win the game (if it’s the final boss) or advance to the next monster.
Weapon Breakage: Sometimes, your weapon might break after an attack, and you lose it from your inventory. But don’t worry, this only happens if you have more than one weapon left.

the code is not passing
the question is this:
Use the logical AND operator && to add a second condition to your if statement. The player’s weapon should only break if inventory.length does not equal (!== ) one.

Something is wrong with your code. It doesn’t match the original code given in this step. Please make your browser do a hard refresh (usually that is CTRL-f5 on windows) and then click reset.
After that add the new condition.

hi there!
you have modified the previous condition within the if statment. you previously had the condition (Math.random() <= .1) .

the original code;
function attack() {
text.innerText = “The " + monsters[fighting].name + " attacks.”;
text.innerText += " You attack it with your " + weapons[currentWeaponIndex].name + “.”;
health -= getMonsterAttackValue(monsters[fighting].level);
if (isMonsterHit()) {
monsterHealth -= weapons[currentWeaponIndex].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.”;
currentWeaponIndex–;
}
}

now you need to add the second condition, using && AND opretor.

if (Math.random() <= 0.1 && inventory.length !== 1) {
text.innerText += " Your " + inventory.pop() + " breaks.";
currentWeaponIndex–;
}
}
it is still not passing

actually you have changed the > more than opretor to <= less than or equal opretor for the first condition. correct that and your code will pass. remember you need only more than > opretor for the fisrt condition comperison.

if (Math.random() > 0.1 && inventory.length !== 1) {
text.innerText += " Your " + inventory.pop() + " breaks.“;
currentWeaponIndex–;
}
}
it still saying " You should not modify your existing if statement.”

i got it now. you need to add the second condition carefully, that`s the fisrt condition should not been modified in any condition. now reset the challenge step and try to add the second condition carefully.

1 Like

thank you … the code passed

1 Like