// groupAnagram(['tea', 'eat', 'tan', 'ate', 'nat','bat'])
// вернуть массив массивов, в которых анаграммы сгруппированы [['tea', 'eat', 'ate',], [ 'nat','tan'], ['bat']]
function groupAnagram(array) {
const result = {};
array.forEach(word => {
const sortedWord = word
.toLowerCase()
.trim()
.split('')
.sort()
.join('');
console.log(sortedWord);
if (result[sortedWord]) {
result[sortedWord].push(word);
} else {
result[sortedWord] = [word];
}
});
console.log(Object.values(result), 'result');
}
console.log(groupAnagram(['tea', 'eat', 'tan', 'ate', 'nat', 'bat']));
Click Run to execute JavaScript.
Code runs in an isolated environment.
Code runs in an isolated environment.