rakesh830566

Character Count

May 28th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.99 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>Assignment 4.14</title>
  7. </head>
  8.  
  9. <body style="font-family: consolas;">
  10.     <h3>Write a program to Print Repeated Character of String</h3>
  11.     <hr>
  12.     <script src="text/javascript">
  13.         var userInput = prompt("Enter a String to Count each Characters...");
  14.  
  15.         var splittedArray = userInput.split('');
  16.  
  17.         var repeatedCharacter = {};
  18.  
  19.         for (i = 0; i < splittedArray.length; i++) {
  20.            if (splittedArray[i] in repeatedCharacter) {
  21.                repeatedCharacter[splittedArray[i]]++;
  22.            } else {
  23.                repeatedCharacter[splittedArray[i]] = 1;
  24.            }
  25.        }
  26.        var characterCounter = Object.entries(repeatedCharacter);
  27.        characterCounter = characterCounter.sort();
  28.        for (i in characterCounter) {
  29.            document.write(characterCounter[i][0] + ' - ' + characterCounter[i][1] + '<br>');
  30.         }
  31.     </script>
  32. </body>
  33.  
  34. </html>
Add Comment
Please, Sign In to add comment