Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Take input two strings from user
- var userFirstInput = prompt('Enter a First String...');
- var userSecondInput = prompt('Enter a Second String...');
- // Displaying strings which is entered by user
- document.write('Entered string by you = <strong>' + userFirstInput + '</strong><br>');
- document.write('Entered string by you = <strong>' + userSecondInput + '</strong><br><br>');
- // Check whether given strings are Anagram or not
- var firstSplittedArray = userFirstInput.toLowerCase().split('').sort();
- var secondSplittedArray = userSecondInput.toLowerCase().split('').sort();
- var notAlphabetRegEx = /[^a-z]+/;
- while (notAlphabetRegEx.test(firstSplittedArray[0])) {
- firstSplittedArray.splice(0, 1);
- }
- while (notAlphabetRegEx.test(secondSplittedArray[0])) {
- secondSplittedArray.splice(0, 1);
- }
- var bool = false;
- if (firstSplittedArray.length === secondSplittedArray.length) {
- for (i in firstSplittedArray) {
- bool = firstSplittedArray[i] === secondSplittedArray[i];
- if (bool === false) {
- break;
- }
- }
- }
- // Displaying strings are Anagram or not
- if (bool) {
- document.write('String <strong>' + userFirstInput + '</strong> and String <strong>' + userSecondInput + '</strong>' + ' are Anagram of each and other.');
- } else {
- document.write('String <strong>' + userFirstInput + '</strong> and String <strong>' + userSecondInput + '</strong>' + ' are not Anagram of each and other.');
- }
Add Comment
Please, Sign In to add comment