diff --git a/06 - Type Ahead/index-START.html b/06 - Type Ahead/index-START.html index 109c90f..545767f 100644 --- a/06 - Type Ahead/index-START.html +++ b/06 - Type Ahead/index-START.html @@ -1,22 +1,60 @@ - - - Type Ahead 👀 - - - + + + Type Ahead 👀 + + + +
+ + +
+ - + const findMatches = (wordsToMatch, cities) => + cities.filter( + (place) => + place.city.match(new RegExp(wordsToMatch, "gi")) || + place.state.match(new RegExp(wordsToMatch, "gi")) + ); + + function displayMatches() { + const matches = findMatches(this.value, cities); + suggestions.innerHTML = matches + .map((match) => { + const regex = new RegExp(this.value, "gi"); + const cityName = match.city.replace( + regex, + `${this.value}` + ); + const stateName = match.state.replace( + regex, + `${this.value}` + ); + return `
  • + ${cityName}, ${stateName} + ${match.population} +
  • `; + }) + .join(""); + } + + const searchInput = document.querySelector(".search"); + const suggestions = document.querySelector(".suggestions"); + + searchInput.addEventListener("change", displayMatches); + searchInput.addEventListener("keyup", displayMatches); + +