I have a class called TreeNode
class TreeNode {
  constructor(name) {
    // Name of the node visible to user. It is string
    self.name = name;
    //  Children of the node. it is array of TreeNode
    self.children = [];
    // If this field is true, children of the Node are shown to the user
    self.expanded = false;
    // If this field is true, it means that the node matches the current search and it is emphasized
    self.matched = false;
  }
The task is to perform the following: /* This function returns a new subtree of the original tree which satisfies the following requirements:
Function doesn't modify the original tree
The 'Node matches the searched' term means that Node's name contains the 'search' as a substring (case insensitive)
Node is included in the resulting subtree if Node, one of its ancestors, or one of its descendants matches the search
If Node matches the search, its matched property must be set to true, otherwise false
If at least one descendant of the Node matches the search, Node's expanded property must be set to true, otherwise false
@returns TreeNode | null */
makeTreeForSearchQuery(search) { // Do Something here return null; } }
I have a function within the class
makeTreeForSearchQuery(search)
{
if (self.children != null)
    {
        for(var i=0; i < self.children.length; i++)
        {
            self.children[i]= makeTreeForSearchQuery(search);
            if(children.matched[i] == true)
            {
                //self.parent = children.matched[i];
                //self.expanded = true;
            }
            
            //Something needs to be done here
        }
    }
        if(self.name === search)
        {
            self.matched = true;
            console.log(self.name);
        }
    
    return TreeNode;
}
I need to get this result: Search = 'two'
Result tree:
root - matched:false, expanded:true
left - matched:false, expanded:true
two - matched:true, expanded:false
Example
 * Original tree
 *       root
 *      |    \
 *    left  right
 *   |   |  \    \
 * one two three four
 *
 * Search = 'two'
 
 Result tree:
 *     root - matched:false, expanded:true
 *      |
 *    left - matched:false, expanded:true
 *      |
 *     two - matched:true, expanded:false
 Or if we describe it in JSON format
 Original tree
  {
  name: "root",
  expanded: false,
  matched: false,
  children: [
    {
      name: "left",
      expanded: false,
      matched: false,
      children: [
        {
          name: "one",
          expanded: false,
          matched: false,
          children: [],
        },
        {
          name: "two",
          expanded: false,
          matched: false,
          children: [],
        },
      ],
    },
    {
      name: "right",
      expanded: false,
      matched: false,
      children: [
        {
          name: "three",
          expanded: false,
          matched: false,
          children: [],
        },
        {
          name: "four",
          expanded: false,
          matched: false,
          children: [],
        },
      ],
    },
  ],
};
Result Tree:
{
  name: "root",
  expanded: true,
  matched: false,
  children: [
    {
      name: "left",
      expanded: true,
      matched: false,
      children: [
        {
          name: "two",
          expanded: false,
          matched: true,
          children: [],
        },
      ],
    },
  ],
}