From c202e725abbf36a1a228417315238b617bcbf2a7 Mon Sep 17 00:00:00 2001 From: kopareigns <kopareigns@gmail.com> Date: Thu, 2 May 2019 11:41:18 -0400 Subject: [PATCH] jsEither improvement --- src/js/generateMarketSlave.js | 3 +-- src/js/utilJS.js | 12 ++++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/js/generateMarketSlave.js b/src/js/generateMarketSlave.js index a311d8a78f0..80c810fa5db 100644 --- a/src/js/generateMarketSlave.js +++ b/src/js/generateMarketSlave.js @@ -73,8 +73,7 @@ window.generateMarketSlave = function(market = "kidnappers", numArcology = 1) { if (V.corpSpecRaces.length === 12) { r += `There is no racial element to trainee selection. `; } else if (V.corpSpecRaces.length === 1) { - const corpSpecRace = jsEither(V.corpSpecRaces); - r += `The corporation specializes in ${corpSpecRace} slaves. `; + r += `The corporation specializes in ${V.corpSpecRaces[0]} slaves. `; } else { r += `The corporation selects slaves based on race. `/*getting into the specifics of which races exactly is a hellhole I'd rather not jump into*/ ; } diff --git a/src/js/utilJS.js b/src/js/utilJS.js index 69e26e93bdc..5968ba64cc1 100644 --- a/src/js/utilJS.js +++ b/src/js/utilJS.js @@ -1148,10 +1148,14 @@ window.jsRandomMany = function(arr, count) { return result; }; -// This function wants an array - which explains why it works like array.random(). Give it one or you'll face a NaN -window.jsEither = function(choices) { - let index = Math.floor(Math.random() * choices.length); - return choices[index]; +// accepts both an array and a list, returns undefined if nothing is passed. +window.jsEither = function(choices, ...otherChoices) { + if (otherChoices.length === 0 && Array.isArray(choices)) { + return choices[Math.floor(Math.random() * choices.length)]; + } + const allChoices = otherChoices; + allChoices.push(choices); + return allChoices[Math.floor(Math.random() * allChoices.length)]; }; /* -- GitLab