Created
October 7, 2021 05:33
-
-
Save juelvaldivia/f4079a5b69b9b04d7a2403ae82a55715 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
// 1. Write a function that evaluates if that word is compund or not (it should recieve an string as input). | |
// 2. Compound words examples: hotdog, goodbye, newspaper. | |
// 3. Not compound words: catdog, hello, news | |
const wordDictionary = [ | |
'good', | |
'bye', | |
'news', | |
'paper', | |
'hot', | |
'dog', | |
'goodbye', | |
'hotdog', | |
'newspaper', | |
'new', | |
'cat' | |
]; | |
// write your function here. | |
const wordExitsInDictionary = (wordsDictionary, word) => wordsDictionary[word] ? true : false; | |
const mapWordDictionary = dictionary => { | |
return dictionary.reduce((acc, cur) => { | |
acc[cur] = true; | |
return acc; | |
}, {}); | |
}; | |
function isCompoundWord(word, wordDictionary) { | |
const wordDictionaryMap = mapWordDictionary(wordDictionary); | |
let fullWord = ''; | |
let currentWord = ''; | |
for(let i=0; i < word.length; i++) { | |
currentWord += word[i]; | |
if (wordExitsInDictionary(wordDictionaryMap, currentWord)) { | |
const letterReamining = word.substring(i+1, word.length); | |
if (wordExitsInDictionary(wordDictionaryMap, letterReamining)) { | |
fullWord = currentWord + letterReamining; | |
break; | |
} | |
} | |
} | |
return wordExitsInDictionary(wordDictionaryMap, fullWord); | |
} | |
console.log(isCompoundWord('dog', wordDictionary)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment