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

207
Visualizações
getting weird result when trying to copy an array using slice() on VS Code

I'm doing the "Chunky Monkey" practice on freeCodeCamp and I'm trying to debug the program on VS Code. But I'm getting a different result than the one I get when I try to run it on the fCC console. fCC returns the expected result for the case, the original array,, but I get a different output on VS Code. Here's the code I made:

function chunkArrayInGroups(arr, size) {
    let groupArr = arr.slice(0);   // VS Code returns [4, 5, 6, 7]
    //console.log(groupArr)
    const groupTarget = Math.ceil(arr.length / size);
    console.log(groupTarget)
    let group = 1;
    console.log(group)
    let finalArr = [];
    for (let i = 0; i < size; i++) {  // iterates though the array 'size' times 
      if (group == groupTarget) {     // if for when we are on the last array to fill
        for (let j = 0; j < size; j++)  // for to iterate size amount of times to see if there is no undefined
        {
          if (groupArr[j] == undefined) { //if there is a undifined value, we'll make a smaller array for the last group 
            finalArr.push(groupArr.splice(0, j))
            console.log(finalArr)
          }
        }
      }
      finalArr.push(groupArr.splice(0, size))  // if we are not on the last group, we just splice the original value and push it 
      console.log(finalArr)
    } 
    return finalArr; // once loops end, we return the finalArray
  }
  
  chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4); 

Hope someone is able to help. Did I mess up something on VS Code? I only copied the exact code that I had.

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