Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda