Last active
June 29, 2021 13:50
-
-
Save BenMansley/9548c64e1b11b5c1eede5d3f939d0b3c to your computer and use it in GitHub Desktop.
Function to get the closest country population to a given number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const populations = { | |
'China': 1439323776, | |
'India': 1380004385, | |
'United States': 331002651, | |
'Indonesia': 273523615, | |
'Pakistan': 220892340, | |
'Brazil': 212559417, | |
'Nigeria': 206139589, | |
'Bangladesh': 164689383, | |
'Russia': 145934462, | |
'Mexico': 128932753, | |
'Japan': 126476461, | |
'Ethiopia': 114963588, | |
'Philippines': 109581078, | |
'Egypt': 102334404, | |
'Vietnam': 97338579, | |
'DR Congo': 89561403, | |
'Turkey': 84339067, | |
'Iran': 83992949, | |
'Germany': 83783942, | |
'Thailand': 69799978, | |
'United Kingdom': 67886011, | |
'France': 65273511, | |
'Italy': 60461826, | |
'Tanzania': 59734218, | |
'South Africa': 59308690, | |
'Myanmar': 54409800, | |
'Kenya': 53771296, | |
'South Korea': 51269185, | |
'Colombia': 50882891, | |
'Spain': 46754778, | |
'Uganda': 45741007, | |
'Argentina': 45195774, | |
'Algeria': 43851044, | |
'Sudan': 43849260, | |
'Ukraine': 43733762, | |
'Iraq': 40222493, | |
'Afghanistan': 38928346, | |
'Poland': 37846611, | |
'Canada': 37742154, | |
'Morocco': 36910560, | |
'Saudi Arabia': 34813871, | |
'Uzbekistan': 33469203, | |
'Peru': 32971854, | |
'Angola': 32866272, | |
'Malaysia': 32365999, | |
'Mozambique': 31255435, | |
'Ghana': 31072940, | |
'Yemen': 29825964, | |
'Nepal': 29136808, | |
'Venezuela': 28435940, | |
'Madagascar': 27691018, | |
'Cameroon': 26545863, | |
'Côte d\'Ivoire': 26378274, | |
'North Korea': 25778816, | |
'Australia': 25499884, | |
'Niger': 24206644, | |
'Taiwan': 23816775, | |
'Sri Lanka': 21413249, | |
'Burkina Faso': 20903273, | |
'Mali': 20250833, | |
'Romania': 19237691, | |
'Malawi': 19129952, | |
'Chile': 19116201, | |
'Kazakhstan': 18776707, | |
'Zambia': 18383955, | |
'Guatemala': 17915568, | |
'Ecuador': 17643054, | |
'Syria': 17500658, | |
'Netherlands': 17134872, | |
'Senegal': 16743927, | |
'Cambodia': 16718965, | |
'Chad': 16425864, | |
'Somalia': 15893222, | |
'Zimbabwe': 14862924, | |
'Guinea': 13132795, | |
'Rwanda': 12952218, | |
'Benin': 12123200, | |
'Burundi': 11890784, | |
'Tunisia': 11818619, | |
'Bolivia': 11673021, | |
'Belgium': 11589623, | |
'Haiti': 11402528, | |
'Cuba': 11326616, | |
'South Sudan': 11193725, | |
'Dominican Republic': 10847910, | |
'Czech Republic (Czechia)': 10708981, | |
'Greece': 10423054, | |
'Jordan': 10203134, | |
'Portugal': 10196709, | |
'Azerbaijan': 10139177, | |
'Sweden': 10099265, | |
'Honduras': 9904607, | |
'United Arab Emirates': 9890402, | |
'Hungary': 9660351, | |
'Tajikistan': 9537645, | |
'Belarus': 9449323, | |
'Austria': 9006398, | |
'Papua New Guinea': 8947024, | |
'Serbia': 8737371, | |
'Israel': 8655535, | |
'Switzerland': 8654622, | |
'Togo': 8278724, | |
'Sierra Leone': 7976983, | |
'Hong Kong': 7496981, | |
'Laos': 7275560, | |
'Paraguay': 7132538, | |
'Bulgaria': 6948445, | |
'Libya': 6871292, | |
'Lebanon': 6825445, | |
'Nicaragua': 6624554, | |
'Kyrgyzstan': 6524195, | |
'El Salvador': 6486205, | |
'Turkmenistan': 6031200, | |
'Singapore': 5850342, | |
'Denmark': 5792202, | |
'Finland': 5540720, | |
'Congo': 5518087, | |
'Slovakia': 5459642, | |
'Norway': 5421241, | |
'Oman': 5106626, | |
'State of Palestine': 5101414, | |
'Costa Rica': 5094118, | |
'Liberia': 5057681, | |
'Ireland': 4937786, | |
'Central African Republic': 4829767, | |
'New Zealand': 4822233, | |
'Mauritania': 4649658, | |
'Panama': 4314767, | |
'Kuwait': 4270571, | |
'Croatia': 4105267, | |
'Moldova': 4033963, | |
'Georgia': 3989167, | |
'Eritrea': 3546421, | |
'Uruguay': 3473730, | |
'Bosnia and Herzegovina': 3280819, | |
'Mongolia': 3278290, | |
'Armenia': 2963243, | |
'Jamaica': 2961167, | |
'Qatar': 2881053, | |
'Albania': 2877797, | |
'Puerto Rico': 2860853, | |
'Lithuania': 2722289, | |
'Namibia': 2540905, | |
'Gambia': 2416668, | |
'Botswana': 2351627, | |
'Gabon': 2225734, | |
'Lesotho': 2142249, | |
'North Macedonia': 2083374, | |
'Slovenia': 2078938, | |
'Guinea-Bissau': 1968001, | |
'Latvia': 1886198, | |
'Bahrain': 1701575, | |
'Equatorial Guinea': 1402985, | |
'Trinidad and Tobago': 1399488, | |
'Estonia': 1326535, | |
'Timor-Leste': 1318445, | |
'Mauritius': 1271768, | |
'Cyprus': 1207359, | |
'Eswatini': 1160164, | |
// Countries with population < 1 million have been removed | |
}; | |
const getClosest = (userCount) => { | |
const countriesWithPopulation = Object.entries(populations); | |
let i = 0; | |
let count = countriesWithPopulation[i][1]; | |
while (userCount < count && i < countriesWithPopulation.length - 1) { | |
count = countriesWithPopulation[i++][1]; | |
} | |
if (i === 0) { | |
return countriesWithPopulation[i][0]; | |
} | |
const [nextCountry, nextCountryPop] = countriesWithPopulation[i - 1]; | |
const [prevCountry, prevCountryPop] = countriesWithPopulation[i]; | |
const differenceToNextCountry = Math.abs(userCount - nextCountryPop); | |
const differenceToPrevCountry = Math.abs(userCount - prevCountryPop); | |
if (differenceToNextCountry <= differenceToPrevCountry) { | |
return nextCountry; | |
} | |
return prevCountry; | |
}; | |
console.log(getClosest(6495231)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment