FAQ: Learn TDD With Mocha - Edge Case

This community-built FAQ covers the “Edge Case” 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 Edge Case

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!

The task here is redundant. we do not need to have if condition to check whether the array is empty.


from what I understand: you set the initial value of the reduce method to zero with that second parameter? Therefore even if the array is empty, the value of zero still comes out of the calculation?

I tried throwing an error if the data type of the array was not numbers, but I got stuck along the way. I only managed to make it work using this code in index_test.js:

    it('raises an error if the array passed in is not made of numbers', () => {
      // Setup
      const inputArray = ['lorem', 'ipsum'];
      // Exercise
      const result = () => {
      // Verification
      assert.throws(result, /only use numbers/);

And writing this block in index.js:

    if (typeof inputArray.reduce((sum, value) => {
      return sum + value;
      }) !== 'number') {
      throw new Error('ERROR: only use numbers');

Seems like it could be shorter, but I failed to find a more concise way that passed all tests. Do any of you know a better way?

1 Like

Hey, your challenge seems interesting, i tried to and implement this solution… Not a lot shorter but maybe a bit more readable to know we’re throwing an error if it’s not a number. I used the .some() methods

    if (inputArray.some(el => typeof el !== 'number')) {
      throw new Error('ERROR: only use numbers');

The sort() method will return a boolean true if one of the element in the array is not a number :slight_smile:

1 Like

Thanks @thibault_deverge! That’s a good solution using a method I did not know about.
For anyone interested, here is the MDN documentation for the .some() method:


1 Like

Hey Sebastien,
I know this discussion is old but I just came across it today.
this is a shorter solution:

if (inputArray.some(isNaN) {
      throw new Error('ERROR: only use numbers');

The isNaN() function determines whether a value is “Not a Number” or not

1 Like

The given solution has edge case bugs.

const Calculate = {
  sum(inputArray) {
    if(inputArray.length === 0){
      return 0

    return inputArray.reduce((sum, value) => {
      return sum + value;
  • this doesn’t take into account if the passed in value is not an array
  • the if statement is also unnecessary when reduce can take an inital value of 0
  • there is no handling if a string is mixed in with the array

My solution

const Calculate = {
  sum(inputArray = []) {
    if (!Array.isArray(inputArray)) return 0;
    return inputArray
      .map(el => Number(el))
      .filter(el => !Number.isNaN(el))
      .reduce((acc, curr) => acc + curr, 0);
  • convert any non-numbers, then filter out any that still aren’t number values and sum with initial value of 0

Test case

  it('returns the sum of numbers in array', () => {
      const expectedResult = 5;
      const inputArray = ["A", 3, "C", 1, "1"];
      const result = Calculate.sum(inputArray);

      assert.strictEqual(result, expectedResult);