Advertisement
PowerCell46

Characters in range JS

Nov 21st, 2022
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function charactersInRange(firstChar, secondChar) {
  2.  
  3. let indexOfTheFirstChar = null;
  4. let indexOfTheSecondChar = null;
  5.  
  6. for(let currentIndex = 0; currentIndex <= 126; currentIndex++) {
  7.  
  8.     if(firstChar === String.fromCharCode(currentIndex)) {
  9.     indexOfTheFirstChar = currentIndex;
  10.     }
  11.     if(secondChar === String.fromCharCode(currentIndex)) {
  12.     indexOfTheSecondChar = currentIndex;
  13.     }
  14. }
  15.  
  16. let firstOutcomePrint = "";
  17. let secondOutcomePrint = "";
  18. if (indexOfTheFirstChar < indexOfTheSecondChar) {
  19. for(let start = indexOfTheFirstChar + 1; start < indexOfTheSecondChar; start++) {
  20. firstOutcomePrint += (String.fromCharCode(start)) + " ";
  21. }
  22. console.log(firstOutcomePrint);
  23. } else if (indexOfTheFirstChar > indexOfTheSecondChar) {
  24. for(let start = indexOfTheSecondChar + 1; start < indexOfTheFirstChar; start++) {
  25. secondOutcomePrint += (String.fromCharCode(start)) + " ";
  26. }
  27. console.log(secondOutcomePrint);
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement