Home > Software engineering >  Basic JavaScript - How to randomly compare two properties from an array of objects
Basic JavaScript - How to randomly compare two properties from an array of objects

Time:06-03

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)
    }

CodePudding user response:

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")
}

CodePudding user response:

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")
        }
    }
}
  • Related