简体   繁体   中英

how to make an algorithm that generates totally random arrays with a given input in JavaScript?(generate random emoji, number, character, symbol etc.)

i was wondering if there is any way to generate a random array of strings, numbers, emojis, symbols etc. with JavaScript. what do i mean by that is when user give us an input like: "gh" or "35" or "😂👌" is there anyway to create an algorithm to create every possible random array, like: "gh" and "hg" , "35" and "53" , "😂👌" and "👌😂" ?

  const createPassword = (characterList) => {
    let password = "";
    const characterListLength = characterList.length;
    for (let i = 0; i < props.passwordLength; i++) {
      const characterIndex = Math.round(Math.random() * characterListLength);
      password = password + characterList.charAt(characterIndex);
    }
    return password;
  };

This is the algorithm I created for the password generator project but I'm so confused with the case when the user gives me the character length and type.

the function will look something like this.

function random(n){
    int randomval= Math.floor(Math.random()*n);
    return randomval;
}

here n is the length of the array in which you can put all the elements you want in your string.

this will give you the range of numbers according to the array length. then you can use the function to access the 'emoji' or any string you want to access in the function which you have created above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM