Skip to content

Instantly share code, notes, and snippets.

@Octagon-simon
Created August 24, 2022 06:19
Show Gist options
  • Select an option

  • Save Octagon-simon/bfa77461f530a08ce5ffdef9b61364d2 to your computer and use it in GitHub Desktop.

Select an option

Save Octagon-simon/bfa77461f530a08ce5ffdef9b61364d2 to your computer and use it in GitHub Desktop.
function unscramble(word) {
//import dictionary
const dict = require('an-array-of-english-words');
//return the result of our logic
return(
dict.filter( item => {
//handle reoccurrences
const reOccurrence1 = {}
const reOccurrence2 = {}
//check if their lengths are equal
if(item.length === word.length){
//convert the current item to array and loop through
item.split('').forEach(letter => {
//store the number of reoccurrences of each letter
reOccurrence1[letter] = reOccurrence1[letter] + 1 || 1;
})
//convert word to array and loop through
word.split('').forEach(letter => {
//store the number of reoccurrences of each letter
reOccurrence2[letter] = reOccurrence2[letter] + 1 || 1;
})
//counter to increase on every match found
let match = 0;
for(let key in reOccurrence1){
if(reOccurrence1[key] === reOccurrence2[key]){
match++;
}
}
//return item
return ( (Object.keys(reOccurrence1).length === match) ? item : false );
}
return;
})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment