0

I am trying to create an array of objects that will be populated by random properties. I want the objects all to look like this
{ systemStar: "something random", planets: ["random1", "random2", etc]} Currently I have the following code

const letThereBeLight = () => {
  let universe = []
  const starNumber = 5;
  let randomNumberOfPlanets = Math.floor(Math.random() * 6);
  for (let i = 0; i < starNumber; i++) {
    universe.push({
      systemStar: starList[Math.floor(Math.random() * lengthOfStarList)],
      systemPlanets: [planetList[Math.floor(Math.random() * lengthOfPlanetList)]]
    })

  }

  console.log(universe)
}

This does a great job in creating the objects I want with the following format, however the systemPlanets is only one, instead of a random number. I have tried to do a double for loop but the syntax was eluding me. How can I get an array of random strings inside of my for loop?
Added variables for clarity

let starList = [
  "Red-Giant",
  "Red-Supergiant",
  "Blue-Giant",
  "White-Dwarf",
  "Yellow-Dwarf",
  "Red-Dwarf",
  "Brown-Dwarf",
];
let planetList = [
  "Rocky",
  "Temperate",
  "Ocean",
  "Frozen",
  "Lava",
  "Gas"
];
imstupidpleasehelp
  • 1,618
  • 2
  • 16
  • 36

2 Answers2

1

What you need is create another loop of random number that is less than number of available planets, and create an array of planets in that loop:

let starList = [
  "Red-Giant",
  "Red-Supergiant",
  "Blue-Giant",
  "White-Dwarf",
  "Yellow-Dwarf",
  "Red-Dwarf",
  "Brown-Dwarf",
];
let planetList = [
  "Rocky",
  "Temperate",
  "Ocean",
  "Frozen",
  "Lava",
  "Gas"
];

const letThereBeLight = () => {
  let universe = []
  const starNumber = 5;
  let randomNumberOfPlanets = Math.floor(Math.random() * 6);
  for (let i = 0; i < starNumber; i++) {
    const planets = [];
    // generate list of planets with at least 1 planet
    for(let p = 0, max = ~~(Math.random() * planetList.length - 1) + 1; p < max; p++)
      planets[planets.length] = planetList[~~(Math.random() * planetList.length)];

    universe.push({
      systemStar: starList[Math.floor(Math.random() * starList.length)],
      systemPlanets: planets
    })

  }

  console.log(universe)
}
letThereBeLight();
vanowm
  • 9,466
  • 2
  • 21
  • 37
  • arguably a system could have multiple rocky planets? – pilchard Apr 30 '22 at 21:51
  • what does `~~` mean? – XMehdi01 Apr 30 '22 at 21:52
  • see: [What is the "double tilde" (~~) operator in JavaScript?](https://stackoverflow.com/questions/5971645/what-is-the-double-tilde-operator-in-javascript) (Keep using `Math.floor` for readability.) – pilchard Apr 30 '22 at 21:53
  • 1
    @TAHERElMehdi it's a faster version of `Math.floor()`, in cases where only positive number is need, it's a faster substite. – vanowm Apr 30 '22 at 21:53
1

Try using Array.from({length:5}) to create an array of 5 elements. and replace 5 with a random number by using Math.random() * 5 Math.floor() method to round it down to near number, add add one to at least create one planet. then map them to values in source array `planetList.

let starList = [
  "Red-Giant",
  "Red-Supergiant",
  "Blue-Giant",
  "White-Dwarf",
  "Yellow-Dwarf",
  "Red-Dwarf",
  "Brown-Dwarf",
];
let planetList = [
  "Rocky",
  "Temperate",
  "Ocean",
  "Frozen",
  "Lava",
  "Gas"
];
let lengthOfStarList = starList.length;
let lengthOfPlanetList = planetList.length;
let universe = []
const starNumber = 5;
for (let i = 0; i < starNumber; i++) {
    universe.push({
      systemStar: starList[Math.floor(Math.random() * lengthOfStarList)],
      systemPlanets: Array.from({length:Math.floor(Math.random() * 5)+1}).map(x=>planetList[Math.floor(Math.random() * lengthOfPlanetList)])
    })
}
console.log(universe)
XMehdi01
  • 5,538
  • 2
  • 10
  • 34