Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 16,998 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,882 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.