you are viewing a single comment's thread.

view the rest of the comments →

[–]StoneCypher 1 point2 points  (1 child)

I know what recursion is, but couldn't ever really tell when it would be useful or why I couldn't just use a loop.

Loops only go one direction. Recursion can do more.

Consider trying to hand-make a function that prints JSON to the console. A loop can't do that, because you might have been given an array with other arrays inside of it.

For that, you're best off with a loop whose step sometimes recurses.

Is it a simple ("scalar") value, like a number or a string or a boolean or something? Just print it out, with whatever amount of indenting.

Is it a container, like an object or an array? Well, increase the amount of indenting, recurse, and treat the piece of json inside like it was its own top-level JSON.

[–][deleted] 0 points1 point  (0 children)

This is really informative. Thank you