Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
handling of argument objects in javascript

attempting to create a function that will return an array with the first n elements of a given array. It must have the following handling:

  • should return an array with the first n elements of the array
  • should return an array with the first element if n is not a number, is zero, or negative
  • should return the entire array if n is > length
  • should work on an arguments object
  • should return an empty array if array is not an array

I'm having trouble satisfying the "should work on an arguments object" while using the following code. When I add a line of code to convert the arguments object into an array then it raises an error with "should return an empty array if array is not an array" - because we're automatically converting the argument into an array. Thank you in advance for any help.

_.first = function (array, n) {
  
  array = Array.from(array);   
     
  if (!Array.isArray(array)) return [];
  
  if (isNaN(n) || n <= 0) return [array[0]];
  
  if (array.length - 1 < n) return array;
  
  return array.slice(0, n);
   
};
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!