Created
January 17, 2017 21:16
-
-
Save mrdougwright/736457525bcd34995350b0ebd538597b to your computer and use it in GitHub Desktop.
honey questions
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
// create anagram db | |
// str = "care race bear face heart earth etc" | |
function sortJoin(word) { | |
return word.split('').sort().join('') | |
} | |
function anagram(str){ | |
let words = str.split(' ') | |
let obj = {} | |
words.forEach(function(w) { | |
obj[w] = words.map((p) => { | |
if(sortJoin(w) === sortJoin(p)) { | |
return p | |
} | |
}).join(' ').trim() | |
}) | |
return obj | |
} | |
// return array of uniques | |
// without mutating | |
// x = [2,1,3,3,2,5] | |
// answer = [1,2,3,5] | |
function uniq(arr) { | |
let newArr = [] | |
let sorted = arr.sort((a,b) => a - b) | |
for(var x = 0; x < sorted.length; x++) { | |
if(sorted[x] !== sorted[x - 1]) { | |
newArr.push(sorted[x]) | |
} | |
} | |
return newArr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment