1

I'm new into this but basically I'm trying to make a JavaScript loop, where 2 fighters fight. I made an array with the 2 fighters and a button connected to my JavaScript from HTML. Now I need to make a loop where the fighters hit each other where the damage by 1 fighter is subtracted by the health of the other so on, later I need to display how the fight went in my html. but I don't know where to start I would be thankful for some help. This is what I have done and I don't know what to do after or if it is even right?

var fighters =  [
{
  "name":"Abdi",
  "HP": 100,
  "DMG": 20,
}
{
  "name": "chriz",
  "HP": 100,
  "DMG": 11,
}

]
function myFunction() {
  for (var i = 0; i < fighters.length; i++) {
    fighters[i]
  }
}
8
  • 1
    Why do you need a loop? If fighters[0] hits fighters[1], it's just fighters[1].HP -= fighters[0].DMG; Commented Apr 19, 2021 at 19:47
  • 1
    yeah but viseversa as well so they hit each other Commented Apr 19, 2021 at 20:10
  • 1
    fighters[0].HP -= fighters[1].DMG for the other hit. Commented Apr 19, 2021 at 20:12
  • 1
    Use loops when the number of elements is dynamic or you need to do the same thing to every element of the array. But that's not the case here. Commented Apr 19, 2021 at 20:13
  • 1
    function myFunction() { for (var i = 0; i < fighters.length; i++) { fighters[1].HP -= fighters[0].DMG; fighters[0].HP -= fighters[1].DMG; } something like this??? Commented Apr 19, 2021 at 21:43

2 Answers 2

1

You're doing perfect.

Few things --

  • fighters[i] refers to a specific fighter. You may want to replace that line with some actual logic, for example fighters[i].HP++ would increase their health by one.

  • Your fighters array doesn't have a comma. You need one.

  • You've defined the function that does what you need, but you haven't called it. You may want to call it by adding a line like myFunction();

Also, don't forget to output something or you'll never know what's happening! A lot of people use console.log() for that, e.g. console.log(fighters[i].HP)

(Note: I specifically did not add the logic you mentioned because I believe that's homework ;)

Sign up to request clarification or add additional context in comments.

5 Comments

Where do you talk about one fighter inflicting damage on the other fighter based on their DMG property?
I personally refrain from directly answering homework questions as that would be detrimental to the student's development. Feel free to downvote if you disagree.
That's fine, but your answer doesn't even address anything in the question.
The part about the missing comma should just be a comment, it's clearly a copying error.
And the question says he has "a button connected to my javascript from html". That's how the function is called.
0
function myFunction() {

  while (fighters[0].HP > 0 && fighters[1].HP > 0) {
  fighters[1].HP -= fighters[0].DMG;
  fighters[0].HP -= fighters[1].DMG;
  document.getElementById('Results').innerHTML+= fighters[1].HP ;
  document.getElementById('Results').innerHTML+= fighters[0].HP;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.