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

118
Visualizações
Why won't the last array value be returned?

I have an array with several values and I want to get value stored in the multiple of 3. So, in this case the new array should be 13 and 28. However, I'm only getting 13.

Code:

    // array1 --> [ '5', '10', '13', '15', '20', '28' ]
    var trueLength = array1.length;
    var iCount = 1;
    for(var i = 0; i < trueLength;) {
        if(iCount % 3 != 0) {
            array1.shift();
        } else if (iCount % 3 == 0) {
            array2.push(array1[i]);
            array1.shift();
        }
        iCount += 1;
        if(iCount == trueLength) break;
    }

Output:

[ '13' ]

I don't see anything logically wrong for it to only output 13 which makes no sense.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

array1.shift(); will remove the first element of the array. If you do this while iterating over iCount, you'll be skipping over some elements of the array, since you're both removing items and incrementing the index being iterated over.

Just filter the array by the modulo of the index being iterated over.

const arr = [ '5', '10', '13', '15', '20', '28' ];
const result = arr.filter((_, i) => i % 3 === 2);
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

you got little logical problem at break condition this would do


var array1 = ['5', '10', '13', '15', '20', '28'];
var array2 =[];

var trueLength = array1.length;
var iCount = 1;
    for(var i = 0; i < trueLength;) {
        if(iCount % 3 != 0) {
           
            array1.shift();
        } else if (iCount % 3 == 0) {
              console.log(iCount);
            array2.push(array1[i]);
            array1.shift();
        }
        iCount += 1;
        if(iCount == trueLength+1) break;
    }
    
    console.log(array2[1]);
    

about 4 years ago · Juan Pablo Isaza Relatório

0

If you like ti use the pattern of shift and neccessary pushign the value, you could use two variables to keep track of the length of the rest items and another for calculate the remainder..

const
    array = ['5', '10', '13', '15', '20', '28'];
    
let i = 1,
    l = array.length;

while (l) {
    const value = array.shift();
    if (i % 3 === 0) array.push(value);
    l--;
    i++;
}

console.log(array);

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