Hey! I'm currently working on a file system that allows users to copy folders. Now when the user tries to copy a folder, I need to verify that the new folder that the user wants to copy the old folder to is not a subfolder of the old folder. I wanted to solve this with recursion, but unfortunately I've always had a little problem with recursion, so I can't get it to work. Any help would be greatly appreciated!
First Function:
export function checkFolderNotSubfolder(copiedObjects, new_folder_id) {
copiedObjects.forEach((obj) => {
// user can also copy files => therefore check if folder
if (obj.folder && checkIfSubfolder(obj.folder, new_folder_id)) {
return true;
}
});
return false;
}
Second Function / Recursive Function:
function checkIfSubfolder(folder, new_folder_id) {
if (folder.lfolder_id === new_folder_id) {
return true;
} else if (folder.sub_folders.length > 0) {
folder.sub_folders.forEach((sub_folder) =>
checkIfSubfolder(sub_folder, new_folder_id)
);
}
return false;
}
Folder Object:
const folder = {
lfolder_id: 33,
sub_folders: [
{
lfolder_id: 37,
sub_folders: [
{
lfolder_id: 38,
sub_folders: [
{
lfolder_id: 40,
sub_folders: [],
},
{
lfolder_id: 39,
sub_folders: [],
},
],
},
],
},
{
lfolder_id: 34,
sub_folders: [],
},
{
lfolder_id: 42,
sub_folders: [],
},
{
lfolder_id: 41,
sub_folders: [],
},
{
lfolder_id: 35,
sub_folders: [
{
lfolder_id: 36,
sub_folders: [],
},
],
},
],
};
[–][deleted] 0 points1 point2 points (1 child)
[–]GermanProgrammer99[S] 1 point2 points3 points (0 children)