0

Let's says I have this array : const numbers = [1,2,3,4,5];

And this array: const letters = ['A','B','C'];

I want to put 'letters' entries inside 'numbers' randomly. I don't care about the order of 'letters', but I want 'numbers' to keep the order. The goal is to have this kind of array :

const randomLettersInNumbers = [1, 'B', 2, 3, 'A', 4, 'C', 5];

How can I achieve this ?

1 Answer 1

1
const numbers = [1,2,3,4,5];
const letters = ['A','B','C'];
let randomLettersInNumbers = numbers
letters.forEach((item) => {
  let index = Math.floor(Math.random() * randomLettersInNumbers.length)
  randomLettersInNumbers.splice(index, 0, item)
})
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like charm, Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.