Created
October 27, 2017 08:16
-
-
Save nanomen/0afcb4e42c518899e0241c5b0b7b8b9f to your computer and use it in GitHub Desktop.
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
| (list => { | |
| const | |
| // Преобразуем строки в массивы | |
| listT = list.map(item => item.split('')), | |
| // Проверка на одинаковые эелементы | |
| hasEqual = (item, index, array) => item[0] === array[0][0], | |
| // Поиск значения | |
| // @list - список для поиска | |
| // @listRes - результат поиска | |
| find = (list, listRes) => { | |
| // Если элементы совпали, ищем дальше | |
| if (list.every(hasEqual)) { | |
| return find( | |
| // Удаляем все первые элементы, | |
| // и продолжаем поиск | |
| list.map(item => item.slice(1)), | |
| // ФОрмируем результирующий массив | |
| [...listRes, list[0][0]] | |
| ); | |
| } | |
| // Совпавших элементов нет, отдаем результат | |
| return listRes; | |
| }; | |
| console.log( | |
| find(listT, []) | |
| ); | |
| })([ | |
| 'abcde', | |
| 'abc', | |
| 'abcdefg' | |
| ]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment