From 7169faa3aaf41c2a9f5ebf201ed3b113fc23f2a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Pe=CC=81ault?= Date: Sun, 28 Nov 2021 23:06:22 +0100 Subject: [PATCH] Fix getRandomElement function Now return an array item instead of an index --- src/components/organisms/InteractiveGlobe.svelte | 2 +- src/routes/shop/__layout.svelte | 2 +- src/utils/functions/index.ts | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/organisms/InteractiveGlobe.svelte b/src/components/organisms/InteractiveGlobe.svelte index 30e2e9b..9d24704 100644 --- a/src/components/organisms/InteractiveGlobe.svelte +++ b/src/components/organisms/InteractiveGlobe.svelte @@ -16,8 +16,8 @@ let containerTop: number = 0, containerHeight: number = 0 let observer: IntersectionObserver - const randomContinent = getRandomElement(continents.filter(cont => cont.countries)) const globeResolution = windowWidth > 1440 && window.devicePixelRatio > 1 ? '4k' : '2k' + const randomContinent = getRandomElement(continents.filter((cont: any) => cont.countries)) const markers = locations.map(({ name, slug, country, coordinates: { coordinates }}: any) => ({ name, slug, diff --git a/src/routes/shop/__layout.svelte b/src/routes/shop/__layout.svelte index 7b132f3..9bae9e9 100644 --- a/src/routes/shop/__layout.svelte +++ b/src/routes/shop/__layout.svelte @@ -221,7 +221,7 @@ */ const productAPI = (!page.params.type && !page.params.name) // Get a random product - ? data.posters[getRandomElement(data.posters)] + ? getRandomElement(data.posters) // Get the current product from slug : data.posters.find(({ location }: any) => location.slug === page.params.name) diff --git a/src/utils/functions/index.ts b/src/utils/functions/index.ts index 56eb8a2..6d28ed0 100644 --- a/src/utils/functions/index.ts +++ b/src/utils/functions/index.ts @@ -97,7 +97,8 @@ export const clamp = (num: number, a: number, b: number) => { * Return a random element from an array */ export const getRandomElement = (array: any[]): any => { - return ~~(array.length * Math.random()) + const randomItemIndex = ~~(array.length * Math.random()) + return array[randomItemIndex] }