Script – 22-22 Mar 9

I'm not sure why my sort in the for loop is not working
var subsets = function (nums) {
let res = nums.reduce(
(subsets, value) =>
subsets.concat(
subsets
.map((set) => [value, ...set])
.sort(function (a, b) {
return a - b;
})
),
[[]]
);

for (let i = 1; i < res.length + 1; i++) {
res[i] = res[i].sort(function (a, b) {
return a - b;
});
}

return res;
};
console.log(subsets([1, 2, 3]));
var subsets = function (nums) {
let res = nums.reduce(
(subsets, value) =>
subsets.concat(
subsets
.map((set) => [value, ...set])
.sort(function (a, b) {
return a - b;
})
),
[[]]
);

for (let i = 1; i < res.length + 1; i++) {
res[i] = res[i].sort(function (a, b) {
return a - b;
});
}

return res;
};
console.log(subsets([1, 2, 3]));
It says TypeError: Cannot read properties of undefined (reading 'sort')
S
Script771d ago
although I already did the sort function in the initial let res
let res = nums.reduce(
(subsets, value) =>
subsets.concat(
subsets
.map((set) => [value, ...set].sort(function (a, b) {
return a - b;
}))
),
[[]]
);
let res = nums.reduce(
(subsets, value) =>
subsets.concat(
subsets
.map((set) => [value, ...set].sort(function (a, b) {
return a - b;
}))
),
[[]]
);
But I still want to know why the sort is reading undefined in the for loop
UU
Unknown User771d ago
S
Script771d ago
oh my bad fvck me thanks a lot
UU
Unknown User770d ago