Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!