Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.

var subsets = function(nums) {
    //add one more digit to each of existing, add that set to existing
    function add(arr,num) {
        const fixLength=arr.length;
        //arr.push([num]);
        for (let i=0;i<fixLength;i++) {
            arr.push(arr[i].concat(num));
        }
        //no return
    }
    let arr=[];
    arr.push([]);
    for (let i=0;i<nums.length;i++) {
        //send each element and append
        add(arr,nums[i]);
        //console.log(i,arr);
    }
    return arr;
};

Approaches:
The strategy here is to look for patterns in what we’re doing.
For each number available, we add a digit as a stand-alone set and that same digit to all other sets.

Code Breakdown:

    //add one more digit to each of existing, add that set to existing
    function add(arr,num) {
        const fixLength=arr.length;
        //arr.push([num]);
        for (let i=0;i<fixLength;i++) {
            arr.push(arr[i].concat(num));
        }
        //no return
    }

This is a helper function. For given array and a number, it will add that number to each of the existing elements in array argument.
For example, if the array contained:

Array:
[]
[1]
[2]
[1,2]

Input:
3

Result is original array PLUS:
[3] <- adding 3 to []
[1,3] <- adding 3 to [1]
[2,3] <- adding 3 to [2]
[1,2,3] <- adding 3 to [1,2]
    let arr=[];
    arr.push([]);
    for (let i=0;i<nums.length;i++) {
        //send each element and append
        add(arr,nums[i]);
        //console.log(i,arr);
    }
    return arr;

This is the main logic of the function. We start off with an empty array and add an empty array as the first set (since the empty set is included).
Then for each number available, we call the helper function with that number. The helper function for every existing set in array, creates a new set with the new number added at the end.
After all numbers are processed, we return the array which is our answer.