From 177a450d1b219ca8ae756e3ce4246579586bf25c Mon Sep 17 00:00:00 2001 From: Matthew Huntington Date: Wed, 19 Apr 2023 13:05:59 -0400 Subject: [PATCH] no global leaves var --- random.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/random.js b/random.js index 2e030c5..57461dc 100644 --- a/random.js +++ b/random.js @@ -43,20 +43,21 @@ const tree = breakDown({value:5}); console.dir(tree, {depth:null}); -const getLeaves = (node, leafArray)=>{ +const getLeaves = (node)=>{ if(node.twoPath === undefined){ return node.value; } - const leftValue = getLeaves(node.twoPath.left, leafArray) - const rightValue = getLeaves(node.twoPath.right, leafArray) - leafArray.push([leftValue, rightValue]); + const twoLeftValue = getLeaves(node.twoPath.left); + const twoRightValue = getLeaves(node.twoPath.right); + if(node.threePath !== undefined){ - const leftValue = getLeaves(node.threePath.left, leafArray) - const rightValue = getLeaves(node.threePath.right, leafArray) - leafArray.push([leftValue, rightValue]); + const threeLeftValue = getLeaves(node.twoPath.left); + const threeRightValue = getLeaves(node.twoPath.right); + return [[twoLeftValue, twoRightValue], [threeLeftValue, threeRightValue]]; } + + return [twoLeftValue, twoRightValue]; } -const leaves = [] -getLeaves(tree, leaves); +const leaves = getLeaves(tree); console.log(leaves);