Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda