题解 | #智能提示#
智能提示
http://www.nowcoder.com/practice/efccf73b4ceb4446b6d71127f874e6d3
function suggest(items) {
let input = document.querySelector('.js-input').value.trim();
let div = document.querySelector('.js-suggest');
let ul = document.querySelector('ul');
ul.innerHTML = ''
var inputArr = input.split("")
if (!input) div.classList.add('hide')
let arr = items.slice(0)
arr.forEach((item, index) => {
var flag = inputArr.length < 1 ? false : true
inputArr.forEach(i => {
if (item.indexOf(i) !== -1) {
item = item.slice(item.indexOf(i)+1)
flag = flag && true
} else {
flag = flag && false
}
console.log(flag)
})
if (flag) {
let tip = document.createElement('li')
tip.innerText = items[index]
ul.appendChild(tip)
}
})
div.appendChild(ul)
ul.children.length ? div.classList.remove('hide') : div.classList.add('hide');
}