From cd4f0fee26d31cae956e046f60617f1057e353b8 Mon Sep 17 00:00:00 2001 From: Tyler Lawson Date: Thu, 18 Feb 2021 23:08:37 -0500 Subject: [PATCH] 6 done --- 06 - Type Ahead/index-START.html | 72 ++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 17 deletions(-) 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); + + -- knotmirror 0.0.1