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 trialTrevor Maltbie
Full Stack JavaScript Techdegree Graduate 17,021 PointsLess 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
Thayer Y
29,789 PointsHi,
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
3,189 PointsI think thats a matter of readabilty, not that is worse or better, just personal preference
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Pointsconst 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.