1

I have an exercise where from an object constructor, I have created several objects, all with an ID property.

The first part of the exercise consist in pairing each object, compare their markAv properties and print the one that had it bigger.

[ a vs b => b wins]

They suggested to do so by using the ID property but I didn't know how to do it that way... so I tried a workaround, as you will see in the code below.

However, the second part of the exercise wants me to do the same but, this time, creating the pairs randomly. Here I have tried to use the ID property by generating a random number that matches the ID but I don´t know how to structure the code for it to work.

The output for the second part should be the same as above, with the only difference that the pairing is now randomly generated.

I have added a partial solution for the second part, partial because from time to time it throws an error that I can´t identify. However, I think I'm getting close to get my desired output.

I would really appreciate if anyone could give me a hint for me to crack the code below and to get it working because I really want to understand how to do this.

class Avenger {
  constructor(name, classRoom, city, job, studies, markAv, id) {
    this.name = name;
    this.classRoom = classRoom;
    this.city = city;
    this.job = job;
    this.studies = studies;
    this.markAv = markAv;
    this.id = id;

  }

  heroList() {
    return this.name + " " + this.classRoom + " " + this.city + " " + this.job + " " + this.studies + " " + this.markAv + " " + this.id
  }

}

const tonyStark = new Avenger("Tony Stark", "XI", "NYC", "Ingeneer", "MIT", 10, 1)
const hulk = new Avenger("Hulk", "X", "Toledo", "Destroyer", "Scientific", 7, 2)
const daredevil = new Avenger("Daredevil", "IX", "NYC", "Lawyer", "Fighter", 2, 3)
const magneto = new Avenger("Magneto", "XXI", "SBD", "Unemployed", "Driver", 5, 4)
const unknown = new Avenger("Unknown", "L", "CDY", "President", "Clerck", 17, 5)
const xavi = new Avenger("Xavi", "XX", "BCN", "Analist", "Calle", 7, 6)


let heroes = [daredevil, hulk, tonyStark, magneto, unknown, xavi]


function getPairs(array) {

  function getPairs(array) {
    for (let i = 0; i < array.length; i += 2) {
      if (array[i].markAv < array[i + 1].markAv) {
        console.log(array[i].name + " vs " + array[i + 1].name + " " + array[i + 1].name + " Wins")
      } else if (array[i].markAv > array[i + 1].markAv) {
        console.log(array[i].name + " vs " + array[i + 1].name + " " + array[i].name + " Wins")
      }
    }
  }

  getPairs(heroes)

///

  function randomAv(array) { 
      let result = []
      let hero1 = heroes[Math.floor(Math.random() * 6) + 1]
      for(let i = 0; i<array.length; i++){
      if (array[i].markAv <= hero1.markAv && array[i].id != hero1.id) {
        result.push(console.log(array[i].name + " vs " + hero1.name + " " + array[i].name + " Wins"))
      } else if(array[i].markAv >= hero1.markAv && array[i].id != hero1.id) {
        result.push(console.log(array[i].name + " vs " + hero1.name + " " + hero1.name + " Wins"))
      }
    }
    console.log(result)
    }

  • Math.floor(Math.random() * 6) + 1 will output range from 0 - 6, should be 0 - 5, try change to: ### Math.floor(Math.random() * heroes.length) ### – Jupri Jun 02 '22 at 15:22

2 Answers2

0

You can take the function from here https://stackoverflow.com/a/7228322/1117736

And do something like this:

class Avenger {
  constructor(name, classRoom, city, job, studies, markAv, id) {
    this.name = name;
    this.classRoom = classRoom;
    this.city = city;
    this.job = job;
    this.studies = studies;
    this.markAv = markAv;
    this.id = id;

  }

  heroList() {
    return this.name + " " + this.classRoom + " " + this.city + " " + this.job + " " + this.studies + " " + this.markAv + " " + this.id
  }

}

const tonyStark = new Avenger("Tony Stark", "XI", "NYC", "Ingeneer", "MIT", 10, 1)
const hulk = new Avenger("Hulk", "X", "Toledo", "Destroyer", "Scientific", 7, 2)
const daredevil = new Avenger("Daredevil", "IX", "NYC", "Lawyer", "Fighter", 2, 3)
const magneto = new Avenger("Magneto", "XXI", "SBD", "Unemployed", "Driver", 5, 4)
const unknown = new Avenger("Unknown", "L", "CDY", "President", "Clerck", 17, 5)
const xavi = new Avenger("Xavi", "XX", "BCN", "Analist", "Calle", 7, 6)


let heroes = [daredevil, hulk, tonyStark, magneto, unknown, xavi]

function randomIntFromInterval(min, max) { // min and max included 
  return Math.floor(Math.random() * (max - min + 1) + min)
}

let hero1 = heroes[randomIntFromInterval(1, 6)]
let hero2 = heroes[randomIntFromInterval(1, 6)]

if (hero1.markAv < hero2.markAv) {
  console.log(hero1.name + " vs " + hero2.name + " " + hero1.name + " Wins")
} else if(hero1.markAv > hero2.markAv) {
  console.log(hero1.name + " vs " + hero2.name + " " + hero2.name + " Wins")
}
Harrys Kavan
  • 741
  • 9
  • 28
  • Hi, thanks for your answer. I thought about a similar solution like the one you wrote but this is not exactly what I am trying to achieve here. The output I'm chasing for would be to get all the 6 objects (or more) from the array randomly paired, without repeating pairs, and compared. The output should be the same as the code that I managed to make it work but in a random order. – pastordesoles Jun 02 '22 at 12:30
0

First shuffle the array:

let heroes = [daredevil, hulk, tonyStark, magneto, unknown, xavi]
let heroes_shuffle = heroes.sort((a, b) => 0.5 - Math.random())

Then do as normal

getPairs(heroes_shuffle )

All Possible Combination :

function allPairs(heroes) {
    while (heroes) {
        [hero, ...heroes] = heroes
        for (enemy of heroes) {
            if (hero.markAv === enemy.markAv)
                console.log(hero.name + " vs " + enemy.name + ": draw")
            else if (hero.markAv < enemy.markAv)
                console.log(hero.name + " vs " + enemy.name + ": " + enemy.name + " Wins")
            else
                console.log(hero.name + " vs " + enemy.name + ": " + hero.name + " Wins")
        }
    }
}
Jupri
  • 345
  • 1
  • 6
  • This is a very good solution but this is not what I'm trying to do. Although I know it's more mechanical, I have edited my main post code whit what I'm trying write. I need to refine it because it's giving me errors and the pairs don't work the way I want. – pastordesoles Jun 02 '22 at 14:49
  • I've added all possible combination – Jupri Jun 02 '22 at 16:32
  • I've tried your code and it´s really good! However I don´t quite understand this part: `[hero, ...heroes] = heroes`. Whatd does the `...heroes` mean? This will help understand how the loop you created works. – pastordesoles Jun 03 '22 at 07:12
  • Its similar to `hero = heroes.pop(0)` but without removing item from original heroes that supplied to the function – Jupri Jun 03 '22 at 07:44
  • Its get the first hero then put the rest to new heroes list – Jupri Jun 03 '22 at 07:54
  • I'm finding it a little bit complex yo understand but the `...` here would be a rest parameter and not a spread operator, right? – pastordesoles Jun 03 '22 at 08:24
  • Yes `...` in this case is the rest parameter. https://replit.com/@zwarag/Rest-Parameter – Harrys Kavan Jun 03 '22 at 11:28