swati-jagtap / javascript-practice

0 stars 0 forks source link

9.1.18--Use reduce to concatenate all the countries and to produce this sentence: Estonia, Finland, Sweden, Denmark, Norway, and IceLand are north European countries #113

Closed swati-jagtap closed 3 years ago

swati-jagtap commented 3 years ago
const countriesReduce=countries.reduce(concate)
function concate(result,current){
  return result+current; 

}

console.log(countriesReduce);``

image


//9.1.18--Use reduce to concatenate all the countries and to produce this sentence: 
//Estonia, Finland, Sweden, Denmark, Norway, and IceLand are north European countries
//const countriesReduce=countries.reduce((accu,curr)=>console.log(accu,curr))
const countriesReduce=countries.reduce(concate)
function concate(result,current,i){       //result points 1st element,current points next/2nd element,i is index of current
  console.log(result)
 console.log(i)
 console.log(current)
 if(i==4){
   return result+" and "+current+" are north European countries "

 }
 else{
  return result+","+current; 
 }
}

console.log(countriesReduce);

image


const countriesReduce=countries.reduce(concate)
function concate(result,current,i){       //result points 1st element,current points next/2nd element,i is index of current
  //console.log(result)
// console.log(i)
 //console.log(current)
 if(i==4){
   return result+" and "+current+" are north European countries "

 }
 else{
  return result+","+current; 
 }
}

console.log(countriesReduce);

image