Skip to content
Advertisement

How return count result instead closure function?

I have a function that returns boolean if tree has at least one enebled value:

  treeHasEnabledNode(): Function {
    let enabled = false;

    return function isNodeEnabled(node: T): boolean {
      if (!node || !node.children || !node.children.length) return enabled;
      if (node.enabled && node.enabled !== undefined) return true;

      node.children.forEach((node: T) => {
        enabled = isNodeEnabled(node);
      });

      return enabled;
    };
  }

Using is:

let hasEnabled = treeHasEnabledNode()(this.tree);

How to return result not calling outher functon (this.tree)?

Advertisement

Answer

You can go about this a few ways. The most simple one would probably be to invoke the internal function within the outer function, and return the result:

function treeHasEnabledNode(node) {
  let enabled = false;

  function isNodeEnabled(node) {
    // do whatever. for example:
    return enabled
  }
    
  return isNodeEnabled(node);
}

const node = {};
console.log(treeHasEnabledNode(node));

However, as @sledetman mentioned in the comments below your question, the code snippet you provided does not “return a boolean if tree has at least one enabled value”.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement