0

I am doing some exercises for a coding school and one of those is reimplementing _.first from Underline. My code is not passing one last test, which is "Should return an empty array if array is not an array", which should seem easy.

However, there is another test which is "Should work on an arguments object" and if I pass the second test, I cannot pass the first one and I cannot think of any other implementation atm. Here is my code for both of these tests:

function (array, n) {
  let resultingArray = [];
  let args = Array.prototype.slice.call(arguments, 0, 1);
  let args2 = args[0];
  if (!Array.isArray(array)) {
    if (array.hasOwnProperty('length')) {
        for (let key in args2) {
          if (args2[key] == 'a' || args2[key] == 'b') {
            resultingArray.push(args2[key]);
          }
        }
    } else {
      resultingArray = [];
    }
  } else if (array == undefined) {
    resultingArray = [];
  } else if (n == undefined || n <= 0) {
    resultingArray = array.slice(0, 1);
  } else {
    resultingArray = array.slice(0, n);
  }
  return resultingArray;
};

The test gives me back the following:

should return an empty array if array is not an array ‣
TypeError: Cannot read property 'hasOwnProperty' of undefined
    at Object._.first (index.js:14:15)
    at Context.<anonymous> (test/test.js:34:9)_.first().should.eql([]);
_.first(null).should.eql([]);
_.first(1).should.eql([]);

Would appreciate any help on the subject, thanks in advance!

10
  • [].hasOwnProperty('length'); is true Commented Feb 7, 2019 at 10:15
  • "if (!Array.isArray(array))" doesn't make sense Commented Feb 7, 2019 at 10:23
  • What is going on with those args and args2? AFAICS, args2 === array. Commented Feb 7, 2019 at 10:24
  • Is your function complete? Because it's missing the closing brace and a return statement too. Commented Feb 7, 2019 at 10:25
  • 1
    @oniramarf it's not complete, I am going to update to include all the code Commented Feb 7, 2019 at 10:27

1 Answer 1

1

The reason the problem exists, is because you aren't checking if array is null or undefined before using it as an object in array.hasOwnProperty('length'). Doing the change shown below should fix the problem.

(array.hasOwnProperty('length')) => (array != null && array.hasOwnProperty('length'))

Note that != is used rather than !== so that both null and undefined will get checked. If you only want to loop through an Object, then you can instead use this check typeof array === "object".

Finally, I would recommend using typeof array[Symbol.iterator] === "function" instead of array.hasOwnProperty('length'). To see if the property can actually be looped for, rather than checking if it has a length property. (This is assuming that you are using ES6 though, since symbols don't exist in ES5)

Sign up to request clarification or add additional context in comments.

5 Comments

I cannot define array or n as it is defined by external sheet test.js, as seen here TypeError: Cannot read property 'hasOwnProperty' of undefined at Object._.first (index.js:14:15) at Context.<anonymous> (test/test.js:34:9) As for the changing the if (!Array.isArray(array)) to if (Array.isArray(array)) I have other tests to pass relating to arrays and my code passes it, I have trouble with these 2 tests specifically and they both concern other elements than arrays (arguments or objects or primitives)
Then how about changing if (array.hasOwnProperty('length')) { to if (typeof array !== "undefined" && array.hasOwnProperty('length')) { ?
Also, I've edited the answer. Can you define array and n like that?
No, I cannot touch the array or n at all. I've checked for the typeof array !== 'undefined' and it works, but the test then throws an error as it cannot read 'hasOwnProperty' of null either, I am going to try going your way and will get back to you
I decided to rearrange the order of the conditionals and then using your logic added !== undefined, !== null and typeof array !== 'number' and yes, it now passes all of the tests! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.