I am currently doing my coding bootcamp course and I’m quite stuck on the first function that I need to implement which needs to pass a series of tests which I have included below.
Now, I know how the arguments object works and how it’s accessed, but I’m struggling to figure out exactly what the test wants me to do with the arguments object.
Now, I’m not asking for a complete solution to the problem since the course provider strictly doesn’t condone using AI or other resources to find solutions to the problems, also this specific test for the arguments object is in other functions I need to implement. I want to grasp an understanding of what exactly the test is asking me to implement and maybe be given advice that will put my in the right direction?
At the moment, I’m checking if the array passed to the function as an argument is an arguments object, if so, I’m converting the array-like object into a real array, then assigning it to the array
parameter.
Below is the current state of my code:
_.first = function (array, n) {
if (!Array.isArray(array)) {
if (Object.prototype.toString.call(array) == '[object Arguments]') {
array = Array.prototype.slice(array);
} else {
return [];
}
}
if (typeof n != 'number' || n == 0 || n < 0) {
return array.slice(0, 1);
} else {
return array.slice(0, n);
}
};
1