From 7be7dfafdd75f775b0bcf7f9d1ad1481b3e92dbe Mon Sep 17 00:00:00 2001 From: "E. R. C. Pereda" Date: Sun, 11 Dec 2016 18:44:40 -0300 Subject: [PATCH] Create the regex outside the filter and map callbacks --- 06 - Type Ahead/index-FINISHED.html | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/06 - Type Ahead/index-FINISHED.html b/06 - Type Ahead/index-FINISHED.html index 5902b43936..fe7c2d48ed 100644 --- a/06 - Type Ahead/index-FINISHED.html +++ b/06 - Type Ahead/index-FINISHED.html @@ -23,11 +23,10 @@ .then(data => cities.push(...data)); function findMatches(wordToMatch, cities) { - return cities.filter(place => { - // here we need to figure out if the city or state matches what was searched - const regex = new RegExp(wordToMatch, 'gi'); - return place.city.match(regex) || place.state.match(regex) - }); + const regex = new RegExp(wordToMatch, 'gi'); + + // here we need to figure out if the city or state matches what was searched + return cities.filter(place => place.city.match(regex) || place.state.match(regex)); } function numberWithCommas(x) { @@ -36,8 +35,8 @@ function displayMatches() { const matchArray = findMatches(this.value, cities); + const regex = new RegExp(this.value, 'gi'); const html = matchArray.map(place => { - const regex = new RegExp(this.value, 'gi'); const cityName = place.city.replace(regex, `${this.value}`); const stateName = place.state.replace(regex, `${this.value}`); return `