Home > Back-end >  AssertionError [ERR_ASSERTION]: undefined == 390 in gitlab
AssertionError [ERR_ASSERTION]: undefined == 390 in gitlab

Time:12-13

i am bit confused with AssertionError [ERR_ASSERTION]: undefined == 390 in gitlab .

i want the follow :

Τhe sumSalaries (obj) function, to accept as a parameter the object obj where the field names correspond to the name of an employee, while the corresponding values ​​are o salary of each employee.

Take the following piece of code :

export default function sumSalaries(obj) {


  let salaries = {
    John: 100,
    Jane: 160,
    Mike: 130
  };

  let sum = 0;
  for (let key in salaries) {
    if (salaries.hasOwnProperty(key)) {
      sum  = salaries[key];
    }
  }
  obj = sum;
  console.log(obj);

}

sumSalaries();

The test must do the following :

import sumSalaries from "../test.js";
import assert from "assert";

describe("\n\ntest.js", () => {
  it("should return the correct sum", () => {
    [
      [
        {
          John: 100,
          Ann: 160,
          Pete: 130,
        },
        390,
      ],
      [
        {
          John: 80,
          Jane: 160,
          Mike: 190,
        },
        430,
      ],
      [
        {
          Charlie: 84,
          Victor: 160,
          Pete: 200,
        },
        444,
      ],
    ].map((obj) => {
      let salaries = obj[0];
      let sum = obj[1];
      assert.equal(sumSalaries(salaries), sum);
    });
  });
});

The error that gitlab gives me :

AssertionError [ERR_ASSERTION]: undefined == 390

CodePudding user response:

Let's take a look at what you've written:

export default function sumSalaries(obj) {

  let salaries = {
    John: 100,
    Jane: 160,
    Mike: 130
  };

Your function is supposed to calculate the sum of the salaries passed in, but you've just put your own fixed list of them in here. Your function will therefore only ever calculate the sum of these three salaries.

You then proceed to add up these salaries, which seems to go okay:

  let sum = 0;
  for (let key in salaries) {
    if (salaries.hasOwnProperty(key)) {
      sum  = salaries[key];
    }
  }

The parameter obj contained the salaries passed in, but after this line it now holds the sum of the salaries you calculated from your fixed list:

  obj = sum;

Then you write this sum to the console. Perhaps useful for debugging, but once you've finished debugging, remove this line.

  console.log(obj);

The function ends without encountering a return statement, so your function returns undefined. This explains why you are getting assertions about undefined == 390: the undefined is what your function returned and the 390 was what your test was expecting to return.

}

So how can you fix this?

Firstly, change the name of the function parameter obj to salaries and get rid of the hard-coded salaries. This allows your function to calculate the sum of the salaries it is given.

Secondly, once you've calculated the sum, return it.

You should end up with the following, which, having tested it, appears to work:

export default function sumSalaries(salaries) {
  let sum = 0;
  for (let key in salaries) {
    if (salaries.hasOwnProperty(key)) {
      sum  = salaries[key];
    }
  }
  return sum;
}
  • Related