swati-jagtap / javascript-practice

0 stars 0 forks source link

9.2.7--Find out which letter is used many times as initial for a country name from the countries array (eg. Finland, Fiji, France etc) #126

Closed swati-jagtap closed 3 years ago

swati-jagtap commented 3 years ago
const countries = ['Finland', 'Sweden','France','Fiji', 'Denmark', 'Norway', 'Iceland','India'
                   ,'America','Japan','Nepal','Delhi','China']
const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook']
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const products = [
  { product: 'banana', price: 3 },
  { product: 'mango', price: 6 },
  { product: 'potato', price: ' ' },
  { product: 'avocado', price: 8 },
  { product: 'coffee', price: 10 },
  { product: 'tea', price: ' ' },
]
//9.2.7--Find out which letter is used many times as initial for a country name from the countries array (eg. Finland, Fiji, France etc)
var fL=[];
var maxc=0;
var maxn=" ";
countries.map((f)=>unique(f[0]))
console.log(fL)

fL.map((f)=>maxCount(f))
console.log(maxn,"is used many times as initial for a country")

function maxCount(f){
  var c=0;
  countries.forEach((a)=>{
    if(a.startsWith(f)){
      c++;
    }
  })
  if(c>maxc){
    maxc=c;
    maxn=f;
  }
}

function unique(f){
  console.log(f)
if(fL.indexOf(f)==-1)
{
  fL.push(f)
}
}

image