From 5d16a681b01d7103febd18be7405211f92296c04 Mon Sep 17 00:00:00 2001 From: gauvainboiche Date: Wed, 18 Dec 2024 12:42:29 +0100 Subject: [PATCH] =?UTF-8?q?Transition=20JS=20compl=C3=A8te?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pl_stat.js => plStat.js | 2 +- planets.js | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) rename pl_stat.js => plStat.js (97%) diff --git a/pl_stat.js b/plStat.js similarity index 97% rename from pl_stat.js rename to plStat.js index 34d6cd5..8091bd6 100644 --- a/pl_stat.js +++ b/plStat.js @@ -1,6 +1,6 @@ exports.planetType = { "Tempérée" : { - "population" : 100, + "population" : 100 }, "Glacée" : { "population" : 1 diff --git a/planets.js b/planets.js index ed1bd1f..00224a8 100644 --- a/planets.js +++ b/planets.js @@ -1,6 +1,37 @@ //import { planetPopulationType, planetNamePrefix, planetNameSuffix } from "./pl_stat.js"; -let pl_stat = require("./pl_stat.js"); +let stat = require("./plStat.js"); -console.log("Hey !"); +const getRandomValue = (min, max) => {return Math.floor(Math.random() * (max - min + 1)) + min;} +const Random = (array) => {return array[Math.floor(Math.random() * array.length)];} +const RandomPlanet = (planet) => { + const keys = Object.keys(planet); + return keys[Math.floor(Math.random() * keys.length)]; +} -console.log(pl_stat.planetPopulationType); \ No newline at end of file +const GeneratePlanet = () => { + const planetTypeGeneration = RandomPlanet(stat.planetType); + const planetName = Random(stat.planetNamePrefix) + Random(stat.planetNameSuffix); + const planetPopulationPeople = Random(stat.planetPopulationType); + const planetPopulationNumber = stat.planetType[planetTypeGeneration]["population"] * getRandomValue(5, 15) / 10; + + const planetDescription = + // Keep this formatting, or else play with the console.log of func PlanetLoop + (` +Planète : ${planetName} +Type : ${planetTypeGeneration} +Population : + ${planetPopulationNumber} milliards + Majoritairement ${planetPopulationPeople}`); + return planetDescription; +} + +const PlanetLoop = (number) => { + let count = 1; + while (count <= number) { + const planet = GeneratePlanet(); + console.log(planet + `\n\n------------------------------`); + count++; + } +} + +PlanetLoop(5); \ No newline at end of file