'Why am I getting .slice is not a function? [closed]

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);


Solution 1:[1]

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.

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MORÈ
Solution 2 Samathingamajig