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

251
Visualizações
The end of my for loop is not running in my javascript function

I am trying to recreate the indexOf function in JavaScript. I can not get the return -1 portion to work, but the other parts work fine. It seems like the end of my for loop is not running, as I try to print out "end of loop". I have some test code as well and the outputs below. Any help would be appreciated.

Array.prototype.myIndexOf = function(...args) {
    if(args[1] === undefined){
        for(let i = 0; i < this.length; i++){
            if(this[i] === args[0]){
                return i;
            }
        }
    } else {
        for(let i = args[1]; i < this.length; i++){
            if(this[i] === args[0]){
                return i;
            }
        }
        console.log("end of loop")
        return -1;
    }
};

// TEST

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beasts.indexOf('bison'));
console.log(beasts.myIndexOf('bison'));
// expected output: 1

// start from index 2
console.log(beasts.indexOf('bison', 2));
console.log(beasts.myIndexOf('bison', 2));
// expected output: 4

console.log(beasts.indexOf('giraffe'));
console.log(beasts.myIndexOf('giraffe'));
// expected output: -1

1
1
4
4
-1
undefined
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You're close. Move the return -1; statement outside of the if/else blocks. As it's currently written, you'll only receive the -1 if it doesn't find a match and you passed in two arguments.

Array.prototype.myIndexOf = function(...args) {
    if(args[1] === undefined){
        for(let i = 0; i < this.length; i++){
            if(this[i] === args[0]){
                return i;
            }
        }
    } else {
        for(let i = args[1]; i < this.length; i++){
            if(this[i] === args[0]){
                return i;
            }
        }
    }
    return -1;
};

// TEST

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beasts.indexOf('bison'));
console.log(beasts.myIndexOf('bison'));
// expected output: 1

// start from index 2
console.log(beasts.indexOf('bison', 2));
console.log(beasts.myIndexOf('bison', 2));
// expected output: 4

console.log(beasts.indexOf('giraffe'));
console.log(beasts.myIndexOf('giraffe'));
// expected output: -1

With this change, you will always hit return -1; if there is no match regardless of the number of parameters passed in.

about 4 years ago · Juan Pablo Isaza Relatório

0

In the last test case you are not sending a second argument to the function, so it is the first part of the if-else that will run - the if part. There is no return -1 in that part.

about 4 years ago · Juan Pablo Isaza Relatório
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