Skip to content

Instantly share code, notes, and snippets.

@ogavrushev
Created May 12, 2017 10:18
Show Gist options
  • Save ogavrushev/fb35c782ca510134aaf2cf41b4f42734 to your computer and use it in GitHub Desktop.
Save ogavrushev/fb35c782ca510134aaf2cf41b4f42734 to your computer and use it in GitHub Desktop.
tin-tasks
// 1
let input = [
'вертикаль',
'кильватер',
'апельсин',
'спаниель',
'австралопитек',
'ватерполистка',
'кластер',
'сталкер',
'стрелка'
];
function findAnagrams(input) {
let result = {};
for (let k in input) {
let anagram = input[k].split('').sort().join(''),
anagrams = input.filter(word => {
return anagram === word.split('').sort().join('');
});
result[anagram] = anagrams;
}
return Object.values(result);
}
console.log(findAnagrams(input));
// 2
function toObject(str) {
let result = {},
levels = str.split('.');
for (let i = levels.length; i--;) {
if (levels[i + 1] in result) {
let temp = result;
result = {};
result[levels[i]] = temp;
} else {
result[levels[i]] = null;
}
}
return result;
}
console.log(toObject('a.b.c.d.n'));
// 3
let sum = value => {
let result = 0,
nextVal = next => {
if (next) {
result += next;
return nextVal;
}
return result + value;
};
return nextVal;
}
console.log(sum(1)(2)(10)());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment