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

213
Views
How to make a slice function closer to the native one? In the form in which invoked?

How to make a slice function manually and be invoked similar or very close to the way it is done in JavaScript, for example:

let arr = [0, 1, 2, 3, 4, 5]

JS Function:

  arr = arr.slice(1, 3)  //it returns: [1,2]

The result I want:

 arr = arr.myManualSlice(1, 3) //it returns: [1,2]

If this example above is impossible to do, I would like to do it as closely as possible. The closest I was able to do was like this below:

function myManualSlice(arr, init, end) {
    let result = []

    if (end == null) {

        for (let i = 0; i < arr.length; i++) {
            if (i >= init) {
                result.push(arr[i])
            }
        }
    } else {

        for (let i = 0; i < arr.length; i++) {
            if (i >= init && i < end) {
                result.push(arr[i])
            }
        }
    }

    return result
}


let arr = [0, 1, 2, 3, 4, 5]

My closest result:

arr = myManualSlice(arr, 1, 3) //it returns: [1,2]

NOTE: I'm not interested in how best to make the body of the function as there are tons of different ways to do it and the function is returning what I expect. I'm only interested in doing the function invocation close to the JavaScript slice way. The logic of the function body can only be changed for this purpose.

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!