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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trevor Maltbie
Full Stack JavaScript Techdegree Graduate 16,994 Points

Less code for capitalizedFruit or less reusable?

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

fruits.forEach(fruit => {
  capitalizedFruits.push(fruit.toUpperCase())
})
console.log(capitalizedFruits)

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

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

let fruits = ['apple', 'pear', 'cherry'].map(c => c.toUpperCase())
console.log(fruits)
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
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,841 Points
const fruits = ['apple', 'pear', 'cherry'];
let capFruits =[];
fruits.forEach( fruit=> {
  let capital = fruit.charAt(0).toUpperCase()+fruit.slice(1);
  capFruits.push(capital);
});

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.