Created
May 25, 2022 14:20
-
-
Save ardeay/dfcefad566c3ae2bdbcb3c1f3b89b45c to your computer and use it in GitHub Desktop.
Recursion Example with Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('Simple Recursion Example') | |
function recurse(num){ | |
let newnum = num + 1 | |
if(num < 50){ | |
recurse(newnum) | |
} | |
console.log(newnum) | |
} | |
recurse(0) | |
console.log('Complext Recursion Example with Array of Objects') | |
let arrayOfObjects = { | |
name: 'Starting Object', | |
children: [ | |
{ | |
name: 'Obj 1', | |
children: [ | |
{ | |
name: 'Obj 2:1', | |
children: [ | |
{ | |
name: 'Obj 3:1', | |
children: [ | |
] | |
}, | |
{ | |
name: 'Obj 3:2', | |
children: [] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2:2', | |
children: [ | |
{ | |
name: 'Obj 1', | |
children: [ | |
{ | |
name: 'Obj 2:1', | |
children: [ | |
{ | |
name: 'Obj 3:1', | |
children: [ | |
] | |
}, | |
{ | |
name: 'Obj 3:2', | |
children: [] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2:2', | |
children: [] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2', | |
children: [ | |
{ | |
name: 'Obj 1', | |
children: [ | |
{ | |
name: 'Obj 2:1', | |
children: [ | |
{ | |
name: 'Obj 3:1', | |
children: [ | |
] | |
}, | |
{ | |
name: 'Obj 3:2', | |
children: [] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2:2', | |
children: [ | |
{ | |
name: 'Obj 1', | |
children: [ | |
{ | |
name: 'Obj 2:1', | |
children: [ | |
{ | |
name: 'Obj 3:1', | |
children: [ | |
] | |
}, | |
{ | |
name: 'Obj 3:2', | |
children: [] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2:2', | |
children: [] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2', | |
children: [] | |
} | |
] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2', | |
children: [] | |
} | |
] | |
} | |
] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2', | |
children: [ | |
{ | |
name: 'Obj 1', | |
children: [ | |
{ | |
name: 'Obj 2:1', | |
children: [ | |
{ | |
name: 'Obj 3:1', | |
children: [ | |
] | |
}, | |
{ | |
name: 'Obj 3:2', | |
children: [] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2:2', | |
children: [ | |
{ | |
name: 'Obj 1', | |
children: [ | |
{ | |
name: 'Obj 2:1', | |
children: [ | |
{ | |
name: 'Obj 3:1', | |
children: [ | |
] | |
}, | |
{ | |
name: 'Obj 3:2', | |
children: [] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2:2', | |
children: [] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2', | |
children: [] | |
} | |
] | |
} | |
] | |
}, | |
{ | |
name: 'Obj 2', | |
children: [] | |
} | |
] | |
} | |
] | |
} | |
function recurseArrayOfObjects(obj,depth=0) { | |
let space = ' ' | |
for (i=0; i<depth; i++){ | |
space += '\t' | |
} | |
console.log(`${space} <li>${obj.name}</li>`) | |
if(obj.children.length > 0){ | |
console.log(`${space} <ul>`) | |
obj.children.forEach(childObject => { | |
return recurseArrayOfObjects(childObject,depth+1) | |
}) | |
console.log(`${space} </ul>`) | |
} | |
} | |
recurseArrayOfObjects(arrayOfObjects) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment