Last active
August 29, 2015 14:03
-
-
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.
This file contains hidden or 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
// 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