-3
const array1 = ["A", "b", "C", "D", "f"]
const array2 = ["❤️", "", "", "", ""]
const array3 = ["0", "1", "2", "3", "4", "5"]


function renderItem() {
    let randomI = Math.floor(Math.random() * array1.length)
    console.log(array2[randomI])
}


function generateRandom () {
    let randomValue = ""
    for(let i = 0; i < 4; i++) {
        randomValue += renderItem()
    }
    return randomValue
}

How do I generate random characters from all of the(3) arrays? and stitch them together.

  • What does "all of the arrays of two different arrays" mean? – Barmar Sep 23 '22 at 18:41
  • `renderItem()` doesn't return anything, it just prints the string on the console. What are you expecting to concatenate? – Barmar Sep 23 '22 at 18:41
  • 1
    Why does `renderItem()` use the length of one array then select the value from a different array? – Barmar Sep 23 '22 at 18:42
  • 1
    If you want a random element from multiple arrays, concatenate the arrays and then pick a random element from that combined array. – Barmar Sep 23 '22 at 18:44
  • like if one checkbox is checked, and another one is unchecked. then I want to generate random characters from array1 and array2 – Zishan9 Ahmed Sep 23 '22 at 18:49
  • `let tempArray = array1.concat(array2);` then get a random element from `tempArray` – Barmar Sep 23 '22 at 18:50
  • ok, so do I have to add if condition for check and uncheck – Zishan9 Ahmed Sep 23 '22 at 18:52
  • Yes, use that when you're deciding whether to concatenate the arrays. – Barmar Sep 23 '22 at 19:01
  • I got it. thank you. can you tell me one other thing that is? how can I use at least 2 characters from each of the arrays when rendering them? – Zishan9 Ahmed Sep 23 '22 at 19:58
  • Get at least 2 random characters from one array, and at least 2 random characters from the other array. Put them all in a new array, and shuffle it. – Barmar Sep 23 '22 at 20:01
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 23 '22 at 21:12

1 Answers1

0

maybe do a 2d array? if you don't want to change the original arrays.

const arr = [array1, array2, array3]
function renderItem() {
    //randomly select an array
    let randArrIndex= Math.floor(Math.random() * arr.length)
    //now select a random Item from that array (we need it's length to get a valid index)
    let randItemIndex = Math.floor(Math.random() * arr[randArrIndex].length)
    console.log(arr[randArrIndex][randItemIndex])
}

sorry if I didn't understand the question correctly

EDIT: if you want at least two characters from each array you can do this:

const array1 = ["A", "b", "C", "D", "f"];
const array2 = ["❤️", "", "", "", ""];
const array3 = ["0", "1", "2", "3", "4", "5"];
const arr = [array1, array2, array3];
const minimum = 2;
const arrCount = new Array(arr.length).fill(0);
function renderItem() {
  let randArrIndex;
  do {
    randArrIndex = Math.floor(Math.random() * arr.length);
    //get another index if this array already have the minimum number
    //and there is another array that still didn't reach the minimum
  } while (arrCount[randArrIndex] >= minimum && arrCount.some((e) => e < minimum));

  //increase the count for the selected array index
  arrCount[randArrIndex]++;

  //now select a random Item from that array (we need it's length to get a valid index)
  let randItemIndex = Math.floor(Math.random() * arr[randArrIndex].length);
  return arr[randArrIndex][randItemIndex];
}

function generateRandom() {
  let randomValue = "";
  //!! the string need to be at least equal to: minimum * arr.length
  // otherwise you will have an infinite loop
  for (let i = 0; i < 6; i++) {
    randomValue += renderItem();
  }
  return randomValue;
}
console.log(generateRandom());

basically just making sure at every random value that all your numbers have reached the minimum (2 characters) if not it will keep generating indexes

though this solution will make the result not entirely random as the string will surely have 2 characters from each array at the start of it. what you can do is shuffle the string after finishing to make sure it's completely random

I3B
  • 108
  • 2
  • 6
  • thanks, man. it worked. can you tell me one other thing? that is can I anyhow use at least 2 of the character from every array? when rendering random items. – Zishan9 Ahmed Sep 23 '22 at 19:50
  • @Zishan9Ahmed I updated the answer I will explain more if there is something you didn't understand – I3B Sep 24 '22 at 21:18