Randomizing Linked Elements From Two Arrays Discord.js

Viewed 33

I tried to find the answer for this but essentially I have not been successful, I am trying to a way to have two arrays like the following

{
    "number": ['1', '2', '3', '4']
}; {
    "color": ['Red', 'Yellow', 'Blue', 'Green']
};

Now say I make a function to make the image get selected randomly:

function random(number) {
  return number[Math.floor(Math.random() * number.length)];
}

Now what I wanted to know is if there was a way that I could take the random result from my function and use it to not only get a random number but also get a corresponding color for it, so for 1 I also want Red, for 2 I want yellow.

Specifically, I am trying to create an embed with two fields

.addField('Your Number', random(number))
.addField('Your color',//what would I put here

What would I put in the last section to get the corresponding color based on the value of the random function in the previous field?

Any help or guidance on this is greatly appreciated!

1 Answers

First store the randomly generated number string in a variable like:

const numValue = random(number)

Then based on this number you can get the respective color based on the index like:

const colorValue = color[Number(numValue) - 1]

Here I am assuming that number here are stored sequentially starting from 1 always.

and then you can use it like:

.addField('Your Number', numValue)
.addField('Your color', colorValue)
Related