javascript - How loop through an array, add only string's that start with 'a' to a new array? -


below given question. understand how loop through array. not know how check specific character in each string in array , how copy string's new array?

question: loop through famous array add people names start 'a' favorites:

var favorites = [];    var famous = [ 'alex smith', 'amy whinehouse', 'cameron diaz', 'brad pitt',   'ashton kutcher', 'mark whalberg', 'morgan freeman', 'mila kunis']; 

my answer far:

  var favoriteletter = 'a';   for(var = 0; < famous.length; i++) {     if(famous.indexof(0) === favoriteletter) {       favorites.push(famous[i]);     }   }; 

change

 if(famous.indexof(0) === favoriteletter) 

to

 if(famous[i].charat(0) === favoriteletter) 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -