Skip to content

Instantly share code, notes, and snippets.

@almost
Last active November 22, 2018 14:48
Show Gist options
  • Save almost/86231d372c1087ad662a43d4acc7f27a to your computer and use it in GitHub Desktop.
Save almost/86231d372c1087ad662a43d4acc7f27a to your computer and use it in GitHub Desktop.
class Tree {
constructor(value, left, right) {
this.left = left;
this.value = value;
this.right = right;
}
}
function* iterateTree(tree) {
// ?????
}
var myTree = new Tree(
1,
new Tree(2, new Tree(3), new Tree(4)),
new Tree(5, new Tree(6), new Tree(7))
);
for (let value of iterateTree(myTree)) {
console.log(value);
}
// Desired output:
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// // Bonus question: Can you make this work?:
// //
// for(let value of myTree) {
// console.log(value);
// }
// //
// // Hint1: The Symbol.iterator property of a class is meant to return an iterator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment