Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Array Iteration Methods Array Iteration Examples Using forEach()

Trevor Maltbie
.a{fill-rule:evenodd;}techdegree seal-36
Trevor Maltbie
Full Stack JavaScript Techdegree Graduate 17,020 Points

Less code for capitalizedFruit or less reusable?

const fruits = ['apple', 'pear', 'cherry'];
let capitalizedFruits = [];

fruits.forEach(fruit => {

Is this just as good as what was shown in the video but with less code or is it better to put the fruit toUpperCase() prior to pushing them?

3 Answers

if the case is to shorten your code, so try this

let fruits = ['apple', 'pear', 'cherry'].map(c => c.toUpperCase())
walter palma
walter palma
3,189 Points

I think thats a matter of readabilty, not that is worse or better, just personal preference

Doron Geyer
Doron Geyer
Full Stack JavaScript Techdegree Student 13,896 Points
const fruits = ['apple', 'pear', 'cherry'];
let capFruits =[];
fruits.forEach( fruit=> {
  let capital = fruit.charAt(0).toUpperCase()+fruit.slice(1);

I would use something like this personally. So that it makes all first letters capital so you dont accidentally miss a name and it also only makes the first letter capital which would make more sense.