Public Solving: Creating random candy bags

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

For today's puzzle, we have to generate random candy bags. Santa has asked us to create bags of candy. However, there are some rules.
Our program should accept and the number of bags and number of candies. It should create a bag with a unique ID and the amount of candy. However, each candy should be unique.
Some side rules are:
My main thinking is about making sure the candies are random and unique.
For this, I'm thinking we should shuffle the candies in each loop and return a slice of this shuffled array that should give us enough "random" for this assignment.
As for the unique bag ID's we can simply use a package, and I'll be using the UUID package.
To install it, run the following command.
npm i uuid
Then let's head over to create our script to help Santa!
First of all, I had to import the candies to the script, which I do here:
import candies from '../data/candy.js';
Then let's import the UUID so we can use it later on:
import { v4 as uuidv4 } from 'uuid';
A rule set to default the candy count to three if it was not provided, so I decided to include it in the parameters.
export const generateCandyBags = (bagCount, candyCount = 3) => {}
The first thing we should fail on is the check if the bag count is null. In that case, we should return an empty array.
if (!bagCount) {
return [];
}
The next check is to see if the candy count exceeds the maximum amount of candy types we have.
if (candyCount > candies.length) {
throw 'TOO_MUCH_CANDY_PER_BAG';
}
This will throw an error if the candy count is larger than the candy array length.
Now that we validated the errors, we must create a loop for each bag in our bagCount variable.
Again, I've decided to use the map method and create an empty array to map over.
return [...Array(bagCount)].map((_, i) => {
// Todo
});
Inside we can return an object that includes an id and our candies.
return [...Array(bagCount)].map((_, i) => {
return {
id: uuidv4(),
candies: [],
};
});
As you can see, we use the uuidv4 function to generate a unique random id.
Now we need to randomly shuffle the candies on each of the bags.
const randomizeCandies = candies.sort(() => 0.5 - Math.random());
This will sort the candies in a randomized order.
Once we shuffled our candies, we can simply use the slice function to return the amount we need.
Making the complete function look like this.
return [...Array(bagCount)].map((_, i) => {
const randomizeCandies = candies.sort(() => 0.5 - Math.random());
return {
id: uuidv4(),
candies: randomizeCandies.slice(0, candyCount),
};
});
Let's try to run the test and see if we made it work:

Yes!
Random candy bags for everyone! Do let me know what your solution would be or what you would do differently. 🍫
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter