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! While you're at it, check out some resources Treehouse students have shared here.

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