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 trialRick S
11,273 PointsUnexpected identifier error given to me with next challenge
Not sure what I'm doing wrong:
const phoneNumbers = ["(503) 123-4567", "(646) 123-4567", "(503) 987-6543", "(503) 234-5678", "(212) 123-4567", "(416) 123-4567"];
let numberOf503;
// numberOf503 should be: 3
// Write your code below
numberOf503 = phoneNumbers.reduce( (sum, phoneNumber) => {
if phoneNumber.startsWith('(503') {
sum += 1;
return sum;
}
return sum;
}, 0);
3 Answers
Cameron Childres
11,820 PointsHi Rick,
You're missing the parenthesis around the conditional for your if statement. There's also a tiny typo -- a missing "s" on the end of phoneNumbers
on line 6.
If you add in the parenthesis and correct the typo your code works great:
numberOf503 = phoneNumbers.reduce( (sum, phoneNumbers) => {
if (phoneNumbers.startsWith('(503')) {
sum += 1;
return sum;
}
return sum;
}, 0);
Rick S
11,273 PointsI was thinking the if condition wouldn't need parentheses, so I didn't think to try that. It works once I add those. Thanks, Cameron!
The 's' actually isn't needed for the formal argument of the callback. Since it represents a single element in the array, rather than the entire array being passed each time it iterates, I named it in the singular. This is the pattern I've seen in examples, so I just followed that. But it could be named whatever as long as it matches what is used in the body of the callback.
Cameron Childres
11,820 PointsMy mistake! I was a little too hasty and when I rewrote your if statement I put an 's' on the end myself then noticed a mismatch (of my own creation!). You're absolutely correct, good catch :)
Rick S
11,273 PointsNo problem. You're quick response was very helpful!