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 trialAndrew Phythian
19,747 PointsAlternative Solution
Posting a different solution method to the video (and others already posted). I index the location of the first instance of the letter S in each name in the array and if it was 0 (the first letter), I added the name to the array.
const names = ['Selma', 'Ted', 'Mike', 'Sam', 'Sharon', 'Marvin'];
var sNames = [];
names.forEach(name => {
if(name.indexOf("S") == 0) {
sNames.push(name);
}
})
console.log(sNames);
Returns the following in the console...
[ 'Selma', 'Sam', 'Sharon' ]
1 Answer
tomd
16,701 PointsLooks good.
I tried to fit it all onto one line and came up with this.
names.forEach( name => (name[0] === 'S') ? s.push(name) : 0);
you can make it even shorter, but its pretty unreadable
names.forEach(n=>(n[0]==='S')?s.push(n):0);
Teacher Russell
16,873 PointsTeacher Russell
16,873 Pointshttps://w.trhou.se/2pvtky460u and yet another way........This was the first thing that popped into my head when I paused the video.