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

249
Visualizações
Why am I getting .slice is not a function?

I know that .slice() can only work on arrays and strings. I'm calling .slice() on an array and I am still getting that error. Here is my code:

var subsets = function(nums) {
    let subsets = [];
    subsets.push([]);
    for(let i = 0; i < nums.length; i++) {
        let currentNumber = nums[i];
        for(let j = 0; j < subsets.length; j++) {
            let currentSubset = subsets[j].slice(0);
            let set1 = currentSubset.push(currentNumber);
            subsets.push(set1)
            
        }
    }
    return subsets;
};

Now on the other hand, this works fine if I insert it into the for loop.

const set1 = subsets[j].slice(0); // clone the permutation
set1.push(currentNumber);
subsets.push(set1);

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

0

Array.prototype.push returns a number, the new length of the array.

Per MDN:

Return value

The new length property of the object upon which the method was called.

When you subsets.push(set1), you push a number, and numbers don't have .slice.

You should do subsets.push(currentSubset) instead.

about 4 years ago · Juan Pablo Isaza Relatório

0

let set1 = currentSubset.push(currentNumber);
subsets.push(set1)

Here currentSubset.push() will return lenth of new array and which you store in subsets array. And that you have declared array of array. So in next iteration subsets[1] will be length of array which you stored from set1 not an array. So I guess you missed some logic over there.

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