Advertisement
iccaka

5, 6, 7 or 8 char length of random numbers and/or letters

May 7th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getRandomIntinRange(min, max) {
  2.   min = Math.ceil(min);
  3.   max = Math.floor(max);
  4.   return Math.floor(Math.random() * (max - min + 1)) + min;
  5. }
  6.  
  7. let DEFAULT_END_OF_PROGRAM_MESSAGE = "end of program";
  8. let DEFAULT_WRONG_COMMAND_MESSAGE = "the fuck are you typing";
  9. let DEFAULT_STRING_MIN_SIZE = 5;
  10. let DEFAULT_STRING_MAX_SIZE = 8;
  11. let DEFAULT_LETTER_A_POSITION = 65;
  12. let DEFAULT_LETTER_Z_POSITION = 90;
  13. let lettersArray = [];
  14. let numbersArray = [];
  15.  
  16. for (let i = DEFAULT_LETTER_A_POSITION; i <= DEFAULT_LETTER_Z_POSITION; i++) {
  17.   lettersArray.push(String.fromCharCode(i));
  18. }
  19.  
  20. for (let i = 0; i <= 9; i++) {
  21.   numbersArray.push(i);
  22. }
  23.  
  24. let answer = prompt('', 'next');
  25.  
  26. if (answer === "end") {
  27.   console.log(DEFAULT_END_OF_PROGRAM_MESSAGE);
  28. } else {
  29.   let currentString = [];
  30.   let currentStringSize;
  31.   let currentCharType;
  32.  
  33.   while (answer === "next") {
  34.     currentStringSize = getRandomIntinRange(DEFAULT_STRING_MIN_SIZE, DEFAULT_STRING_MAX_SIZE);
  35.  
  36.     for (let i = 0; i <= (currentStringSize - 1); i++) {
  37.       currentCharType = Math.floor((Math.random() * 2));
  38.       if (currentCharType === 0) {
  39.         currentString.push(lettersArray[getRandomIntinRange(0, (lettersArray.length - 1))]);
  40.       } else {
  41.         currentString.push(numbersArray[getRandomIntinRange(0, (numbersArray.length - 1))]);
  42.       }
  43.     }
  44.  
  45.     console.log(currentString.join(""));
  46.     currentString = [];
  47.  
  48.     answer = prompt('', 'next');
  49.  
  50.     if (answer === "end") {
  51.       console.log(DEFAULT_END_OF_PROGRAM_MESSAGE);
  52.       break;
  53.     }
  54.   }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement