From e766a422355477e0d6580f8e803e014569ba55da Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 29 May 2023 16:48:01 +0100 Subject: [PATCH] Fix bug where the randomiser would pick -1 This was caused by the randomiser using the wrong brackets. We needed the maximum index of the length - 1, but the randomiser would take the 1 AFTER randomising, adding brackets made it do this BEFORE. --- .gitignore | 7 ++++--- src/index.ts | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 65e3fd0..4915027 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -node_modules -dist -yarn-error.log \ No newline at end of file +node_modules/ +dist/ +coverage/ +yarn-error.log diff --git a/src/index.ts b/src/index.ts index bdd157d..400567e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,8 +41,8 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P return { IsSuccess: false, }; - } else if (dataWithImages.length > 1) { - random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1) + } else { + random = Math.floor((Math.random() * (dataWithImages.length - 1)) + 0); // Between 0 and (size - 1) } const randomSelect = dataWithImages[random];