Tests failed to run due to an error: "". Check your code and try again

Hello, I am having trouble with this challenge: https://www.codecademy.com/code-challenges/code-challenge-calculate-the-mean-and-mode-javascript.

Here is my code:

function statsFinder(array) { // Write your code here let retArr = []; const mean = array.reduce((sum, item) => sum + item) / array.length; retArr.push(mean); let modeArr = []; for (let i = 0; i < array.length; i++) { if (modeArr.length === 0) { modeArr.push({ name: array[i], count: 1 }) } else { let myObj; for (let key in modeArr) { if (modeArr[key].name === array[i]) { modeArr[key].count += 1 } else { myObj = { name: array[i], count: 1 } } } modeArr.push(myObj); } } modeArr.sort((a,b) => (a.count < b.count) ? 1 : ((a.count > b.count) ? -1 : 0)) retArr.push(modeArr[0].name); return retArr; } console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300])) //logs [ 368.75, 400 ] // Leave this so we can test your code: module.exports = statsFinder; //

As far as I can tell the code works, I’ve tested it extensively… Any ideas why I’m getting this error (Tests failed to run due to an error: “”. Check your code and try again.)? I think it might be due to the fact that my answer has spaces around the array items… but I’m not sure why that is or how to fix it.

Also any feedback about the code is more than welcome, I’m not sure if it’s needlessly complicated and if there is a much simpler way to do this…

Hello!

One of the problems is on line 25. You always insert myObj into the array, even if you just increment the count in an existing entry.

Try printing modeArr to the console before sorting and you will see entries with duplicate numbers.

1 Like

Hi, thanks for the reply. I’m really stuck on how to fix this. I realise that it creates duplicate objects but I can’t seem to change that no matter how I change my code. I’m not sure why it is a problem anyway if I’m just going to sort the list before returning it…

Hello!

But moving line modeArr.push(myObj); inside loop after creating new object on lines 19-22 fixed error for me.

If I do that like this:

function statsFinder(array) {
  // Write your code here
  let retArr = [];
  const mean = array.reduce((sum, item) => sum + item) / array.length;
  retArr.push(mean);
  let modeArr = [];
  for (let i = 0; i < array.length; i++) {
    if (modeArr.length === 0) {
      modeArr.push({
        name: array[i],
        count: 1
      })
    } else {
      let myObj;
      for (let key in modeArr) {
        if (modeArr[key].name === array[i]) {
          modeArr[key].count += 1
        } else {
          myObj = {
            name: array[i],
            count: 1
          }
          modeArr.push(myObj);
        }
      }
    }
  }
  console.log(modeArr);
  modeArr.sort((a,b) => (a.count < b.count) ? 1 : ((a.count > b.count) ? -1 : 0))
  retArr.push(modeArr[0].name);
  return retArr;
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300])) //logs [ 368.75, 400 ]

// Leave this so we can test your code:
module.exports = statsFinder;

//

Then I push way more objects to the array, I push an object for every nested for loop! You can see by the console.log(modeArr) as you suggested.

Hello!

Yes, but there is no “Tests failed” error with this code and you can go forward and fix other issues. I only wanted to fix this error, not make the code completely correct.

Oh right, I see what you mean, so I should start from there and try to fix the other errors? Should I further clean up the array after this step in the code? Perhaps by iterating over modeArr once more to remove duplicates? I honestly feel completely lost and like I’m just heading in the wrong direction.

First of all, let’s try to fix this fragment:

      let myObj;
      for (let key in modeArr) {
        if (modeArr[key].name === array[i]) {
          modeArr[key].count += 1
        } else {
          myObj = {
            name: array[i],
            count: 1
          }
          modeArr.push(myObj);
        }
      }

If you have found number in modeArr you don’t need to check another items in it. So you can use break to exit the loop:

      let myObj;
      for (let key in modeArr) {
        if (modeArr[key].name === array[i]) {
          modeArr[key].count += 1;
          break; // added line
        } else {
          myObj = {
            name: array[i],
            count: 1
          }
          modeArr.push(myObj);
        }
      }

Then, you need to add new item into the modeArr only if array[i] is not in modeArr. So you need to check if myObj has value and only in this case add it to modeArr after loop:

      let myObj;
      for (let key in modeArr) {
        if (modeArr[key].name === array[i]) {
          modeArr[key].count += 1;
          break;
        } else {
          myObj = {
            name: array[i],
            count: 1
          }
        }
      }
      if (myObj) {
        modeArr.push(myObj); // yes, we moved it outside of the loop again
      }

The original problem arose because your code added undefined myObj to myArr when the function was called with an array of the same numbers, like [500, 500]. The if statement will fix this.

So your solution is workable with some fixes.

You can see other solutions on the challenge forum JavaScript Challenge - Calculate the Mean and Mode, Calculate the Mean and Mode

1 Like

Ahhh, I was sooo close to this! I just couldn’t quite get it to work. I even had the idea of adding the if statement afterwards to check if it should be pushed or not. The only thing I didn’t think of was declaring an unassigned variable and check if it had been edited or not! That’s an excellent solution, thanks for all your help!