I'm very junior, excuse me if it isn't an appropriate.
Trying to count the objects in an array, ignoring null. This is my code so far:
function countTheObjects (arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'object') {
count++;
}
if (arr[i] === null) {
count--;
}
}
return count;
}
What am I doing wrong?
Edit:
All these codes you guys have given me return exactly the same error mine does. These are the tests the code has to pass:
describe('countTheObjects', function () {
it('returns the count of objects inside an array of random data types', function () {
expect(countTheObjects([])).to.equal(0);
expect(countTheObjects([1, 3, 4, 5])).to.equal(0);
expect(countTheObjects([1, 3, 4, 5, 'foo'])).to.equal(0);
expect(countTheObjects([1, 3, 4, 5, {}, {}, {}, 'foo'])).to.equal(3);
expect(countTheObjects([1, [], 3, 4, 5, {}, {}, {}, 'foo'])).to.equal(3);
expect(countTheObjects([1, [], null, 3, 4, 5, {}, {}, {}, 'foo'])).to.equal(3);
expect(countTheObjects([1, {}, [], null, null, 'foo', 3, 4, 5, {}, {}, {}, 'foo'])).to.equal(4);
});
});