swati-jagtap / javascript-practice

0 stars 0 forks source link

9.3.2--Find the 2 most spoken languages: #128

Closed swati-jagtap closed 3 years ago

swati-jagtap commented 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.2--Find the 2 most spoken languages:
const cLang=countries.map((a)=>a["languages"])
const langA=cLang.sort().join(",")
let langa=langA.split(",")
langa.sort()
//console.log(cLang)
//console.log(langA)
console.log(langa)
//const a=langa.forEach((a)=>mostSpoen(a))

let j=[];
let j1=[];
var l;
var c;
var msl={l,c}

for(let a=0;a<langa.length;a++){
  (mostSpoen(langa[a])) 

}

j1.sort((a,b)=>{
  if(a.as>b.as)return -1;
  if(a.as<b.as)return 11;
  return 0;
})
//console.log(j,j1)
console.log("2 most spoken languages are",j1.slice(0,2))

function mostSpoen(d){  
 const count= langa.filter((b)=>b==d)
 //console.log("count",count)
 if(j.indexOf(count[0])==-1){ 
   j.push(count[0]);
  //j1.push(count.length)
  var as=count.length;
  var al=count[0];
  msl={al,as}
  j1.push(msl)
}
 //console.log(msl)
 //console.log(s)
//console.log(count.length)

}

image