Tell us what’s happening:
My code is failing and I can’t spot where/why it fails:
if (monsterHealth <= 0) {
if (fighting === 2) {
winGame();
} else {
defeatMonster();
}
}
The console msg:
You should add a new if statement in your else if block.
Your new if statement should check if fighting is strictly equal to 2.
You should call the winGame function in your if block.
You should add an else block.
Your else block should call the defeatMonster function.
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
// User Editable Region
function attack() {
if (health <= 0) {
lose();
}
text.innerText = "You attack the " + monsters[fighting].name + " with your " + weapons[currentWeaponIndex].name + ".";
monsterHealth -= weapons[currentWeaponIndex].power + Math.floor(Math.random() * xp) + 1;
monsterHealthText.innerText = monsterHealth;
if (monsterHealth <= 0) {
if (fighting === 2) {
winGame();
} else {
defeatMonster();
}
}
health -= monsters[fighting].level;
healthText.innerText = health;
if (health <= 0) {
lose();
} else {
text.innerText += " The " + monsters[fighting].name + " attacks you.";
}
}
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0
Challenge Information:
Learn Basic JavaScript by Building a Role Playing Game - Step 140