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!

5
  • 2
    Better idea: beenToUserCountry = countries.map(a=>a.toLowerCase()) .includes(userCountry.toLowerCase()); Commented Jan 26, 2019 at 19:43
  • Possible duplicate of How to use a variable inside a RegEx pattern? and How do you use a variable in a regular expression? Commented Jan 26, 2019 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. Commented Jan 26, 2019 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 :) Commented Jan 26, 2019 at 20:23
  • Fair enough, thanks Commented Jan 26, 2019 at 21:14

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

You will want to properly escape the user input though if you really want to go down this route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.