So I have data from https://restcountries.com/v3.1/all that gives me 250 countries.
Inside a country, France for example, it lists out all the countries that border it.
borders:(8) ['AND', 'BEL', 'DEU', 'ITA', 'LUX', 'MCO', 'ESP', 'CHE']
Those three character names appear as cca3. So for France its cca3 is ‘FRA’. Every country has a cca3 so when I loop through data, I get all 250 unique cca3 names.
I have
let borders = [];
for (let j = 0; j < data[i].borders.length; j++) {
borders.push(data[i].borders[j]);
}
Which creates an array for all of those countries that border the selected country (again, France here).
console.log(borders, "borders");
returns
(8) ['AND', 'BEL', 'DEU', 'ITA', 'LUX', 'MCO', 'ESP', 'CHE'] 'borders'
I have for (let i = 0; i < data.length; i++) as the main loop. It loops through all 250 countries and prints out info I’m using for the page.
When I loop through borders to find any cca3 names that match between borders[k] and data[i] it pushes the matching cca3 into a new array, matches
So far so good.
Here’s the code that loops and matches
for (let k = 0; k < borders.length; k++) {
if (borders[k] == data[i].cca3) {
matches.push(data[i].name.common);
}
}
The very weird problem is that only five of those eight border countries are pushed into the matches array.
console.log(matches, "matches");
returns
(5) ['Luxembourg', 'Belgium', 'Switzerland', 'Germany', 'Spain']
'matches'

I thought that maybe some specific cca3’s were broken or something, but for example, Laos will show “China” in it’s matches array, but other countries that have borders with China will not show it.
I have absolutely no idea why some of these aren’t matching. China has 15 out of 16 matching, but Russia only has 2 out of 14 matching.
Sweden and Norway will both show Finland as matching, but Russia does not.
const api_url = new URL("https://restcountries.com/v3.1/all");
let data = "";
//Short name of countries bordering selected country
let borders = [];
let matches = [];
async function getData() {
// Make request and get response
const response = await fetch(api_url);
// JSON
data = await response.json();
// Loop through data
for (let i = 0; i < data.length; i++) {
if (data[i].name.common.toLowerCase() == country) {
const lang = Object.keys(data[i].languages)[0];
console.log(data[i], "data");
console.log(data[i].cca3, "cca3");
flagImg.src = data[i].flags.png;
countryName.textContent = data[i].name.common;
nativeName.textContent = data[i].name.nativeName[lang].common;
population.textContent = data[i].population.toLocaleString("en", {
useGrouping: true,
});
region.textContent = data[i].continents;
subRegion.textContent = data[i].subregion;
capital.textContent = data[i].capital;
domain.textContent = data[i].tld;
currency.textContent = Object.values(data[i].currencies)[0].name;
language.textContent = Object.values(data[i].languages);
borders = [...data[i].borders];
}
// Loop through the data and if any short names match with the bordering country short names, add to matches array
for (let k = 0; k < borders.length; k++) {
if (borders[k] == data[i].cca3) {
matches.push(data[i].cca3);
}
}
}
console.log(borders, "borders");
console.log(matches, "matches");