rakesh830566

String Anagram

May 28th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Take input two strings from user
  2. var userFirstInput = prompt('Enter a First String...');
  3. var userSecondInput = prompt('Enter a Second String...');
  4.  
  5. // Displaying strings which is entered by user
  6. document.write('Entered string by you = <strong>' + userFirstInput + '</strong><br>');
  7. document.write('Entered string by you = <strong>' + userSecondInput + '</strong><br><br>');
  8.  
  9. // Check whether given strings are Anagram or not
  10. var firstSplittedArray = userFirstInput.toLowerCase().split('').sort();
  11. var secondSplittedArray = userSecondInput.toLowerCase().split('').sort();
  12.  
  13. var notAlphabetRegEx = /[^a-z]+/;
  14.  
  15. while (notAlphabetRegEx.test(firstSplittedArray[0])) {
  16.     firstSplittedArray.splice(0, 1);
  17. }
  18. while (notAlphabetRegEx.test(secondSplittedArray[0])) {
  19.     secondSplittedArray.splice(0, 1);
  20. }
  21.  
  22. var bool = false;
  23. if (firstSplittedArray.length === secondSplittedArray.length) {
  24.     for (i in firstSplittedArray) {
  25.         bool = firstSplittedArray[i] === secondSplittedArray[i];
  26.         if (bool === false) {
  27.             break;
  28.         }
  29.     }
  30. }
  31.  
  32. // Displaying strings are Anagram or not
  33. if (bool) {
  34.     document.write('String <strong>' + userFirstInput + '</strong> and String <strong>' + userSecondInput + '</strong>' + ' are Anagram of each and other.');
  35. } else {
  36.     document.write('String <strong>' + userFirstInput + '</strong> and String <strong>' + userSecondInput + '</strong>' + ' are not Anagram of each and other.');
  37. }
Add Comment
Please, Sign In to add comment