Created
May 16, 2019 20:54
-
-
Save Lavrend/ddc82d3c903f1b723614896699fec2e7 to your computer and use it in GitHub Desktop.
Checks if words are the same letter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const arrSame = ['va5ya', '5avya', '5ayva']; | |
| const arrDiff = ['cat', 'act', 'car']; | |
| const isUniqChars = (arr) => { | |
| const sortFn = (a, b) => { | |
| if (a > b) return 1; | |
| if (b > a) return -1; | |
| return 0; | |
| }; | |
| const getUniqVal = (value, index, self) => { | |
| return self.indexOf(value) === index; | |
| }; | |
| const sorted = arr.map(item => item.split('').sort(sortFn).join('')); | |
| return (sorted.filter(getUniqVal)).length <= 1; | |
| }; | |
| console.log(isUniqChars(arrSame)); // true | |
| console.log(isUniqChars(arrDiff)); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment