Javascript Question

Which array among 3 arrays contain a specific value and then use that array? Javascript

I have 3 arrays:

northamerica = ["USA","Canada","Mexico"];
europe = ["France", "Germany", "Italy"];
africa = ["Morocco", "Ghana", "Madagascar"];


I want to find which one of these 3 arrays contain a specific value. It should look like this:

Input:

Madagascar


Output:

The countries in the same continent are: Morocco, Ghana, Madagascar


How can I get this result with Javascript?

Answer

You might do as follows;

function getContinentNeighbours(country){
  return country +
         " is in the same continent with: " +
         continents.reduce((p,continent) => continent.includes(country) ? p += continent.filter(f => f !== country)
                                                                                        .reduce((f,s) => f + ", " + s)
                                                                        : p ,"");
}
var northamerica = ["USA","Canada","Mexico"],
          europe = ["France", "Germany", "Italy"],
          africa = ["Morocco", "Ghana", "Madagascar"],
      continents = [northamerica, europe, africa];

console.log(getContinentNeighbours("Mexico"));