# FAQ: Learn TDD With Mocha - Red to Green II

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

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

## FAQs on the exercise Red to Green 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 () 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.

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

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!

Is it typical to use ‘for’ loops over array methods in professional software development or was that just used as an example for this exercise.

I answered my own question as I got to the next step in this lesson

As an alternative, I was looking through Stack Overflow and we could do this:

``````const Calculate = {
sum(inputArray) {
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const sum = inputArray.reduce(reducer);
return sum;
}
}

``````
1 Like

Yes it’s nice, even more concise you can just return :

``````return inputArray.reduce((acc, currV) => acc + currV);
``````

Happy coding !

3 Likes

Two easy ways of doing this:

``````sum(inputArray) {

let totalSum = 0;

inputArray.forEach((element) => totalSum += element)

}
``````

or…

``````sum(inputArray) {

const result = inputArray.reduce((accumulator, currentValue) => accumulator + currentValue);

return result;
}
``````
1 Like

It won’t me pass with this one:

``````const Calculate = {
sum(inputArray) {
const reducer = (previousValue, currentValue) => previousValue + currentValue;
return inputArray.reduce(reducer)
}
}

module.exports = Calculate;
``````

I used this code, which I’ve tested in the terminal and elsewhere, which does work:

``````const Calculate = {
sum(inputArray) {
const result = inputArray.reduce((a, b) => {
return a + b;
})
return result;
}
}
``````

It seems the exercise wants us to specifically use a for…of loop instead of e.g. an array.reduce() method, but this isn’t specified anywhere, so that a little unhelpful when the code still works - surely the exercise’s pass condition should reflect the output of the tests we’re running?

I attempted to solve it using the reduce iterator, but I keep getting an error. I believe I’ve implemented it correctly so it might be a CodeAcademy issue.