1

I'm trying to write code that will allow user to check to see if I have been to a specific country.

I've tried this by getting input from user via prompt. Taking that input, storing it into a variable and then inserting that variable into a regex. From there, I attempted to use the .some method on my array of countries, utilizing the regex and the user input variable to determine whether or not I have been to the country that the user specifies.

var countries = ['US', 'UK', 'Canda', 'Mexico', 'Panama', 'Dominican 
                  Republic', 'Brazil', 'Germany', 'France', 'Portugal',
                  'Spain', 'the Netherlands'];

var userCountry = prompt("Please enter a country", "");

var beenToUserCountry = countries.some(country => 
                        /^userCountry$/i.test(country));

if (beenToUserCountry) {
    document.write(`Yes, I have been to ${userCountry}.`);
  } else {
    document.write(`No, I haven't been to ${userCountry} yet.`);
  }

I expect that the code will print "Yes..." for countries that a part of my countries array and and "No..." for ones that are not. Instead, each country that I insert into the prompt gets me a "No..." Help!

Jevon Cochran
  • 1,565
  • 2
  • 12
  • 23
  • 2
    Better idea: `beenToUserCountry = countries.map(a=>a.toLowerCase()) .includes(userCountry.toLowerCase());` – Niet the Dark Absol Jan 26 '19 at 19:43
  • Possible duplicate of [How to use a variable inside a RegEx pattern?](https://stackoverflow.com/questions/45451533/how-to-use-a-variable-inside-a-regex-pattern) and [How do you use a variable in a regular expression?](https://stackoverflow.com/questions/494035) – adiga Jan 26 '19 at 20:11
  • Sure, it may be a duplicate. I saw the other related threads but as a beginner, I didn't understand the explanations given there. Having folks give explanations based on my own problem/example has helped a lot. – Jevon Cochran Jan 26 '19 at 20:14
  • It's still a duplicate. I'm adding it so that when someone in the future googles something similar and ends up in this question, they will see those popular questions as "linked" (panel on the right side). It might help them :) – adiga Jan 26 '19 at 20:23
  • Fair enough, thanks – Jevon Cochran Jan 26 '19 at 21:14

1 Answers1

2

You can use a RegExp object for that, here is an example:

var countries = ['US', 'UK', 'Canda', 'Mexico', 'Panama', 'Dominican Republic', 'Brazil', 'Germany', 'France', 'Portugal',
                  'Spain', 'the Netherlands'];

var userCountry = prompt("Please enter a country", "");

var beenToUserCountry = countries.some(country => 
                        new RegExp(`^${userCountry}$`, "i").test(country));

if (beenToUserCountry) {
    document.write(`Yes, I have been to ${userCountry}.`);
  } else {
    document.write(`No, I haven't been to ${userCountry} yet.`);
  }

As @Bergi has mentioned, you should probably escape userCountry to remove any special characters from it, you can find a good example of how you can do that here

Titus
  • 22,031
  • 1
  • 23
  • 33
  • 2
    You will want to [properly escape](https://stackoverflow.com/q/3561493/1048572) the user input though if you really want to go down this route. – Bergi Jan 26 '19 at 19:51