Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

248
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda