Javascript Role Playing Game step 156

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.";

  }
} 
  
}

Hi there. Can you include a link to the exercise you are doing to help us understand the context?

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Hi @otoya1one

Decrement the value of currentWeaponIndex in your if statement, after you update the text.

This is the if statement you need to update.

Happy coding

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?

Try to look back at what that if statement seems to be doing.

What is the first thing that it does? Why does it do it?

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?

1 Like

i got it, thanks for your help

2 Likes

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