简体   繁体   中英

Random Array of Objects & Properties

so I have an array in my code below but one of my requirements for the assignment is to have a very LARGE array of objects around like 10,000 objects. My teacher was saying that the computer can just keep generating random objects for you given how many you want so in my case like 10,000. I'm just confused about how I would do that, any help or suggestions are really appreciated thank you!

  class Shoe {
      constructor(name, price, type) {
        this.name = name;
        this.price = price;
        this.type = type;
      }
  }

  var shoes = [
     new Shoe('Nike AirMax 90', '120', 'Casual'),
     new Shoe('Jordan Retro 1', '110', 'Casual'),
     new Shoe('Jadon Doc Martens', '250', 'Seasonal boots'),
     new Shoe('Adidas X Ghosted', '110', 'Athletic'),
     new Shoe('Nike Vapourmax Flyknit', '250', 'Casual'),
     new Shoe('Aldo Loafers', '130', 'Formal'),
     new Shoe('Timberlands', '199', 'Seasonal boots'),
     new Shoe('Converse High Tops', '70', 'Casual'),
     new Shoe('Converse Low Tops', '80', 'Casual'),
     new Shoe('Adidas NMDs', '110', 'Athletic'),
     new Shoe('Heels', '130', 'Formal'),
     new Shoe('Nike AirForce', '150', 'Casual')
  ];

  function bubbleSort(shoes) {
    var swapped;
    do {
      swapped = false;
      for (var i = 0; i < shoes.length - 1; i++) {
        if (+shoes[i].price > +shoes[i + 1].price) {
           var temp = shoes[i];
           shoes[i] = shoes[i + 1];
           shoes[i + 1] = temp;
           swapped = true;
        }
      }
    } while (swapped);
    return shoes;
   }

   bubbleSort(shoes);
   console.log('Bubble Sort:\n', shoes);

That's a perfect job for a loop. You have to create helper functions that generate random names and prices and types.

For the name , we'll use:

function randomName(n) {
  let letters = "abcdefghijklmnopqestuvwxyz";
  let name = "";
  
  for(let i = 0; i < n; i++) {
    name += letters[Math.floor(Math.random() * letters.length)];
  }

  return name;
}

This function generates strings of a specific length, but those strings are just gibberish which I guess is not very important.

For the price , we'll use:

function randomNumber(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}

For the type , we'll just use randomName .

Then we use a for loop to populate the shoes array like so:

var shoes = [];

for(let i = 0; i < 10000; i++) {
  shoes.push(new Shoe(randomName(20), randomNumber(50, 5000), randomName(7)));
}

Demo:

 class Shoe { constructor(name, price, type) { this.name = name; this.price = price; this.type = type; } } function randomName(n) { let letters = "abcdefghijklmnopqestuvwxyz"; let name = ""; for (let i = 0; i < n; i++) { name += letters[Math.floor(Math.random() * letters.length)]; } return name; } function randomNumber(min, max) { return Math.floor(Math.random() * (max - min) + min); } var shoes = []; for (let i = 0; i < 100; i++) { shoes.push(new Shoe(randomName(20), randomNumber(50, 5000), randomName(7))); } console.log(shoes);

You can create an array of 10,000 Shoes each with a random price like this:

 class Shoe { constructor(name, price, type) { this.name = name; this.price = price; this.type = type; } } var names = ["Nike AirMax 90", "Jordan Retro 1", "Jadon Doc Martens"]; // can be more var types = ["Casual", "Athletic", "Formal"]; // can be more var shoes = []; for (var i = 0; i < 10000; i++) { shoes.push(new Shoe( names[Math.floor(Math.random() * names.length) -1], Math.floor(Math.random() * 10000), types[Math.floor(Math.random() * names.length) -1]) ); } console.log(shoes)

This will let you do the bubble sort exercise. The data might not be very realistic but that probably doesn't really matter I assume.

Write a function that creates a random shoe, and call it in a loop.

 class Shoe { constructor(name, price, type) { this.name = name; this.price = price; this.type = type; } } function random_words(length) { let word = ""; const chars = " abcdefghijklmnopqrstuvwxyz"; let last_space = true; for (let i = 0; i < length; i++) { let char = chars[Math.floor(Math.random() * chars.length)]; word += last_space ? char.toUpperCase() : char; last_space = char == " "; } return word; } function random_shoe() { let name = random_words(30); let price = Math.floor(Math.random() * 200); let style = random_words(10); return new Shoe(name, price, style); } let shoes = []; for (let i = 0; i < 10; i++) { shoes.push(random_shoe()); } console.log(shoes);

 class Shoe { constructor(name, price, type) { this.name = name; this.price = price; this.type = type; } } names = ["Nike", "Adidas", "Puma"]; types = ["Running", "Casual", "Walking", "Training"]; pricesRange = [1, 1000]; const randomShoes = []; const numberOfShoes = 10; const randomInteger = () => parseInt(Math.random() * (pricesRange[1] - pricesRange[0]) + pricesRange[0]) const randomArrayItem = arr => { const randomIndex = parseInt(Math.random() * (arr.length - 1)); return arr[randomIndex]; } for (let i = 0; i < numberOfShoes; i++) { const name = randomArrayItem(names); const price = randomInteger(); const type = randomArrayItem(types); const shoe = new Shoe(name, price, type) randomShoes.push(shoe); } randomShoes.forEach(shoe => console.log(shoe));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM