Javascript: Transformer

JS: Trees
Last update: 08 Nov 23:20
0
Students

Before taking this challenge, we recommend that you go through and analyze the teacher's solution in the "Building an itinerary" challenge.

transformer.js

Implement and export as default a function that builds a tree relative to a given root node.

The function takes 2 arguments:

  • source tree
  • the root node for a new tree to built

The function should return a new tree preserving links between nodes, where the passed node is the root.

Examples

const tree = ['A', [ //     A
  ['B', [            //    / \
    ['D'],           //   B   C
  ]],                //  /   / \
  ['C', [            // D   E   F
    ['E'],
    ['F'],
  ]],
]];

transform(tree, 'B');

// ['B', [           //   B
//   ['D'],          //  / \
//   ['A', [         // D   A
//     ['C', [       //      \
//       ['E'],      //       C
//       ['F'],      //      / \
//     ]],           //     E   F
//   ]],
// ]];

Tips

  • Other examples can be found in the tests file
  • Use the lodash lib functions

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests