Skip to content

Instantly share code, notes, and snippets.

@rmariuzzo
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmariuzzo/0d351d2aa88c13c39dbe to your computer and use it in GitHub Desktop.
Save rmariuzzo/0d351d2aa88c13c39dbe to your computer and use it in GitHub Desktop.
printList will take a nested list of unknown depth and print each item on a separate line.
// Proposed solution for an exercise given to a friend: http://pastebin.com/hjFCZYE7
function printList(linePrefix, list) {
for (var i = 0; i < list.length; i++) {
if (typeof list[i] === 'string') {
console.log(linePrefix + '.' + i + ': ' + list[i]);
} else {
printList(linePrefix + '.' + i, list[i]);
}
}
}
var sample = ["Red",["Yellow","Green","Blue"],"Black",[["Orange","Purple"],"White"]];
printList('Test', sample);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment