i can’t figure this out, i put the decrement operator after the currentWeaponIndex-- like so. any help is appreciated
Remember that the increment operator ++can be used to increase a variable's value by1. There is also a decrement operator –that can be used to decrease a variable's value by1`. For example :
Example Code
let num = 10;
num--;
console.log(num); // Output: 9
Decrement the value of currentWeaponIndex in your if statement, after you update the text.`
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.";
}
}
}
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
here’s the link.
And thanks for the help Teller, but i’m still lost on how to add or where to add the currentWeaponIndex-- to the ‘if’ statement you stated. The Math.random() <= .1 function
how does currentWeaponIndex-- fit into that if statement?
first thing it’s doing is giving a random number less than or equal to .1…
after that I can’t figure out what the rest of the if statement is doing or where to add currentWeaponIndex–
so remember we are playing a game. So the random aspect is part of that game, so that the behavior is not predictable. Okay then, that first line of the if says this: text.innerText += " Your " + inventory.pop() + " breaks.";
It seems to be using an incrementing/concatenation operator right? +=
what is it concatenating? I see two strings “Your” and “breaks.” and something in the middle.
So the final string it seems to be making is Your ...... breaks.
What is that middle thing? inventory.pop()
Do you remember this pop()?
What will it create then. And can you figure out the intention of the if based on this string it seems to be making? (something is breaking … what breaks?)
thanks for your help, i kinda rememeber the .pop(), it returns the last element of the array. And i’m guessing what breaks is the currentWeaponIndex, but i’ve tried to add it in in different ways but no luck. i’m following your logic but this one is over my head. i’m not sure what would pop()
pop will remove the last element of the array, the weapons array right? So it will reduce the number of weapons you have when you’re playing cause one broke.
so knowing this, since the weapon you were playing with broke (bad luck), which weapon then are you left holding? This is where the currentWeaponIndex-- comes in.
so therefore, since your weapon changes after the one you were holding has broken, where should the currentWeaponIndex-- go?