FAQ: Learn TDD With Mocha - Refactor II

This community-built FAQ covers the “Refactor II” exercise from the lesson “Learn TDD With Mocha”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn JavaScript Unit Testing

FAQs on the exercise Refactor II

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

For the accumulator part of reduce - I would think we’d need something like totalSum = 0, to keep track of it, but reduce apparently doesn’t require that. Can someone help me understand why?


See the docs for more information in how array.reduce() works, but basically when you have code like so:

const blah = [1, 2, 3];
const sum = blah.reduce((a, b) => a + b);
console.log(sum); // 6

a is initially assigned to the first element of the array.
b is initially assigned to the second element of the array.
The fat arrow is essentially the same as return, so the values are added together and returned
a then gets assigned to that returned value, and b gets assigned to the next element of the array.
This process repeats until there are no elements remaining to assign to b.
You can add an optional initial value. In that case a gets assigned to the initial value first, and b gets assigned to the first element of the array. The above process is then followed.

const blah = [1, 2, 3];
const sum = blah.reduce((a, b) => a + b, 5); // 5 is the initial value
console.log(sum); // 11
1 Like

Thank you for your explanation! I read the documentation, but it’s difficult to digest. Plugging and playing is probably a good idea

1 Like

When you go to array.reduce MDN page (Array.prototype.reduce() - JavaScript | MDN) there is no “return” needed inside the callback function. In fact, if you add it, you get an Unexpected Token error.
However, in this exercise you need to have “return” inside the callback function (and again inside the .sum method to return it out.

I don’t understand how if the MDN example works without “return” (and doesn’t work when its added) - and the value is stored in a constant (i.e. if you copy exactly what the MDN page has), this exercise doesn’t work.