I want to generate an array of objects filled with random properties. Each object should have the following structure:
{ systemStar: "something random", planets: ["random1", "random2", etc]}
Here's the current code I am using:
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)
}
The code successfully creates the desired objects, but the systemPlanets
only contains one instead of a random number. I attempted a double for loop but struggled with the syntax. How can I generate an array of random strings within my for loop?
Additional 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"
];