how to generate a random 4 digit alphanumeric number in javascript that corresponds to a passed 8 digit number in a function

97 views Asked by At

For example if I pass any 8 digit number say 97895614 then it should return some 4 digit number (it can be alphanumeric) in a way that it corresponds only to that passed 8 digit number.

I made a function that takes any 8 digit number as an argument

1

There are 1 answers

0
Sebastien H. On

You can use the String.fromCharCode method for the string prototype : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode.

But since you use 2 digits per 'char' you will be limited in the output characters to the first 100 UTF-16 characters.

i.e :

console.log(String.fromCharCode(189, 43, 190, 61));
// Expected output: "½+¾="

If you need to output a specific set of characters you'll have to add a calculation on top of your input digits, to only include this set.