FAQ: Learn TDD With Mocha - Red To Green I

This community-built FAQ covers the “Red To Green I” 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 Red To Green I

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!

Why must we declare const Calculate = {}; as a constant in this example?

https://www.codecademy.com/paths/back-end-engineer-career-path/tracks/becp-test-driven-development-with-javascript/modules/fecp-learn-tdd-with-mocha/lessons/tdd-with-mocha/exercises/green-i

Must you declare it as a constant? It’s common practice to do so, but as far as JavaScript is concerned, there’s no requirement to use const. I would, and always do use const unless there’s a reason not to. Did you try something else, and receive an error?

1 Like

Yep, used let and had to change it to const to move on

That’s the SCT for the exercise, but there’s nothing syntacticly wrong with using let.

1 Like

Well, when one doesn’t know what one is doing, those types of issues take a hit on ones confidence. :sweat_smile:

Could you clarify what SCT stands for??

Thanks in advance

Submission Correctness Test. The writer of the exercise wrote the SCT to only accept const it would appear.

1 Like

As I said previously, it is a good idea to use const whenever possible. It keeps us from accidentally changing things we don’t want to change. Consider the following:


function blah(a, b) {
  return a + b;
}
console.log(blah(7,9)); // 16
// many lines of code
blah = 'eleventeen' // accidentally overwrote my function
console.log(blah) // eleventeen
//many more lines of code
// try to use the function later
console.log(blah(7, 9)); // TypeError: blah is not a function

//versus

const blahblah = (a, b) => a + b
console.log(blahblah(7, 9)); // 16

blahblah = 'eleventeen' // TypeError: Assignment to constant variable.

1 Like

Here is my index.js

// index.js
const Calculate = {
  sum(x) {
    const reducer = (previousValue, currentValue) => previousValue + currentValue;
    return x.reduce(reducer)
  }
}
module.exports = Calculate;

This is index_test.js

const assert = require('assert');
const Calculate =  require('../index.js')

describe('Calculate', () => {
  describe('.sum',() => {
    it('returns the sum of of a list\'s items', () => {
      assert.equal(Calculate.sum([1,2,3]), 6);
    });
  });
});

The TDD is green (passing), but after pressing the ‘Check Work’ button, it still does not allow to pass.
It errors with

Did you program the .sum() method to return 6?

Can some one help please?

1 Like

For anyone in the future that runs across this, on this step they want you to return 6 no matter what (currently it is Step 4).

You will define the actual code to return the sum based on the numbers passed to the method a few steps later.

1 Like

Is there any particular reason why Phrase is capitalized? I am on the Fullstack Engineer track and, as far as I remember, we’re only supposed to capitalize classes or constructor functions. But Phrase is neither… Can someone please correct me if I am wrong?

Hi everyone, quick question here… Why do I need to pass a parameter in sum() ? (Here, in the exercise, I got an error because I didn’t pass a parameter.

However, after passing a (x) parameter, my test succeeded (passing). Here’s my test code.

const Calculate = { sum(cal) { return 6; } }; module.exports = Calculate;