BACK
PracticeInterview: expand() to walk a tree
PRACTICE · 69 · 練

Interview: expand() to walk a tree

Walk root -> a,b with expand and from(children).

  1. Solve the task in the editor — no long theory.
  2. Run the check and compare your output with the expected result.
  3. Stuck? Open the solution and carry the approach into your own code.
Solution spoiler · click to reveal
const { of, from, expand, take } = Rx;

const tree = {
  root: ['a', 'b'],
  a: [],
  b: [],
};

const result$ = of('root').pipe(
  expand(node => from(tree[node])),
  take(3)
);

result$.subscribe(value => console.log(value));
script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...