I have the following code to display list with words for autocompleting:
var words = ['foo', 'bar', 'barz', 'ba'];
var possibleWords = [];
document.getElementsByTagName('div')[0].addEventListener('keyup', function(){
var textWords = this.innerText.split(' ');
var lastWord = textWords[textWords.length-1];
for(var k = 0; k < words.length; k++){
for(n = 0; n < 20; n++){
if(lastWord.length == n+1 && words[k].charAt(n) == lastWord.charAt(n)){
possibleWords.push(words[k]);
var counts = possibleWords.reduce((a, c) => {
a[c] = (a[c] || 0) + 1;
return a;
}, {});
var maxCount = Math.max(...Object.values(counts));
var mostFrequent = Object.keys(counts).filter(k => counts[k] === maxCount);
}
}
}
console.log(mostFrequent);
var list = this.getElementsByTagName('ul')[0];
if(mostFrequent == undefined) {
list.removeAttribute('style');
} else {
list.style.display = 'inline';
list.innerHTML = '';
list.innerHTML = '<li>'+mostFrequent.join('</li><li>')+'</li>';
}
});
div{
width:300px;
height:100px;
outline:1px solid #ccc;
position:relative;
}
ul{
display:none;
position:absolute;
left:0;
top:100px;
}
<div contenteditable="true"><ul></ul></div>
When I comment the bottom code from var list = ... to list.innerHTML = '<li>'+... and try to type b, then ba, then bar and then barz I see correct result in the console. But if I don't comment the bottom code, I receive undefined half the time and the list is also hidden half the time.
How to resolve the issue?