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 Manipulation Return a Single Value from an Array with reduce()

Unexpected 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
MOD
Cameron Childres
Treehouse Moderator 11,714 Points

Hi 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);

I 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
Cameron Childres
Treehouse Moderator 11,714 Points

My 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 :)

No problem. You're quick response was very helpful!