Closed swati-jagtap closed 3 years ago
const countries = [ { name: 'Afghanistan', capital: 'Kabul', languages: ['Pashto', 'Uzbek', 'Turkmen'], population: 27657145, flag: 'https://restcountries.eu/data/afg.svg', currency: 'Afghan afghani' }, { name: 'Åland Islands', capital: 'Mariehamn', languages: ['Swedish'], population: 28875, flag: 'https://restcountries.eu/data/ala.svg', currency: 'Euro' }, { name: 'Albania', capital: 'Tirana', languages: ['Albanian'], population: 2886026, flag: 'https://restcountries.eu/data/alb.svg', currency: 'Albanian lek' }, { name: 'Algeria', capital: 'Algiers', languages: ['Arabic'], population: 40400000, flag: 'https://restcountries.eu/data/dza.svg', currency: 'Algerian dinar' }, { name: 'American Samoa', capital: 'Pago Pago', languages: ['English', 'Samoan'], population: 57100, flag: 'https://restcountries.eu/data/asm.svg', currency: 'United State Dollar' }, { name: 'Andorra', capital: 'Andorra la Vella', languages: ['Catalan'], population: 78014, flag: 'https://restcountries.eu/data/and.svg', currency: 'Euro' }, { name: 'Angola', capital: 'Luanda', languages: ['Portuguese'], population: 25868000, flag: 'https://restcountries.eu/data/ago.svg', currency: 'Angolan kwanza' }, { name: 'Anguilla', capital: 'The Valley', languages: ['English'], population: 13452, flag: 'https://restcountries.eu/data/aia.svg', currency: 'East Caribbean dollar' }, { name: 'Antarctica', capital: '', languages: ['English', 'Russian'], population: 1000, flag: 'https://restcountries.eu/data/ata.svg', currency: 'Australian dollar' }, { name: 'Antigua and Barbuda', capital: "Saint John's", languages: ['English'], population: 86295, flag: 'https://restcountries.eu/data/atg.svg', currency: 'East Caribbean dollar' }, { name: 'Argentina', capital: 'Buenos Aires', languages: ['Spanish', 'Guaraní'], population: 43590400, flag: 'https://restcountries.eu/data/arg.svg', currency: 'Argentine peso' }, { name: 'Armenia', capital: 'Yerevan', languages: ['Armenian', 'Russian'], population: 2994400, flag: 'https://restcountries.eu/data/arm.svg', currency: 'Armenian dram' } ] //9.3.3--Use countries_data.js file create a function which create the ten most populated countries function mostPopulated(countries,n){ countries.sort((a,b)=>{ if(a.population>b.population)return -1; if(a.population<b.population)return 1; return 0; }) let country;let population; const answer=[]; //let ans={country,population} const nArray = countries.slice(0,n); const a=nArray.map((n)=>{ country=n["name"]; population=n["population"] ans={country,population} //console.log("aaa",ans) answer.push(ans) }) return answer; } console.log("the ten most populated countries",mostPopulated(countries,10))