Skip to content

Instantly share code, notes, and snippets.

@SergProduction
Created August 15, 2017 13:16
Show Gist options
  • Save SergProduction/4e4811218117f2292eed4b829e34327f to your computer and use it in GitHub Desktop.
Save SergProduction/4e4811218117f2292eed4b829e34327f to your computer and use it in GitHub Desktop.
var dictinary = {};
function addWord(word, value) {
value = value ? value : word
var letters = word.split('')
var newDic = letters.reduce((prev, curr, i) => {
if (letters.length -1 === i) {
return prev[curr] = value
}
return prev[curr] ? prev[curr] : prev[curr] = {}
}, dictinary)
}
function getWord(val) {
var key = val.split('')
var step = dictinary
for (let i=0; i<key.length; i++) {
try { step = step[key[i]] }
catch(err){ return false }
}
return step ? step : false
}
var words = [
'ФАВНА', 'ФАВОРИТ', 'ФАЗ',
'ФАЗАН', 'ФАЗИС', 'ФАКЕЛ',
'ФАКИР', 'ФАКСИМИЛЕ', 'ФАЛ',
'ФАЛАЛЕЙ', 'ФАЛАНГА', 'ФАЛДА']
words.forEach(word => addWord(word))
console.log('getWord', getWord('ФАКЕЛ'))
console.log('getWord', getWord('ФАКЕЛs'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment