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 Manipulation Practice reduce()

Lainey Odette
Lainey Odette
18,618 Points

Can't get passed code challenge - feedback not helpful

Hello, I'm trying to take the code challenge at the end of the Array Manipulation section of the JavaScript Array Iteration Methods course. I followed the instructions for the challenge and my code works (it even says so when I tell it to check my work.) It is telling me the reason why I'm wrong is because I didn't use reduce() in my code. But I did. So I'm not sure what I did wrong. I tried it with .startsWith and .includes. Neither option works according to the challenge. I'm not sure what else to try. I haven't learned a different method to look at just the first few characters in a string. It's not part of this course. My research led me to .startsWith and .includes. So now I'm stuck. How does Treehouse want me to resolve this so I can pass the challenge? (Even though my code works and I've used the reduce() method as required.) Thank you.

app.js
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

const howMany = phoneNumbers.reduce((total, codeCheck) => {
  if (codeCheck.startsWith("(503)")) {
      return total + 1;
  } 
  return total;
}, 0);

numberOf503 = howMany;
Lainey Odette
Lainey Odette
18,618 Points

I was finally able to fix it by not creating the new constant "howMany" and just assigning it to numberOf503 right away. I got thrown off when it asked me to create the code below the comments. I withdraw my question. Thank you!

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Nice job, your code works as is-- unfortunately the code-challenge grader isn't quite smart enough to recognize it.

What Steven says is "the grader is distracted by the extra variable" is correct.

I changed your code, ever so slightly and it worked.

Making a direct assignment of your variable "numberOf503" from the reduce function makes it pass the auto-grader.

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((total, codeCheck) => {
  if (codeCheck.startsWith("(503)")) {
      return total + 1;
  } 
  return total;
}, 0);
Lainey Odette
Lainey Odette
18,618 Points

Thanks for the help and explanation! This is very helpful. I definitely prefer clean code and I will be sure not to get into the habit of creating extraneous variables!

Steven Parker
Steven Parker
231,007 Points

The challenge is apparently evaluating the line where the variable "numberOf503" gets assigned. Creating the extra variable apparently "distracted" it from seeing your (correct) method.

FYI, this might be a good place to use the compact form of the arrow function combined with a ternary:

numberOf503 = phoneNumbers.reduce((tot, num) => num.startsWith("(503)") ? tot + 1 : tot, 0);
Lainey Odette
Lainey Odette
18,618 Points

Thanks, Steven! Haven't learned that compact form yet, but it's much prettier!!