Script
Script3y ago

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')
4 Replies
Script
ScriptOP3y 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
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Script
ScriptOP3y ago
oh my bad fvck me thanks a lot
reactibot
reactibot3y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/951243719884042270 This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/951243719884042270

Did you find this page helpful?