//const numBeats = Math.ceil(Math.random()*9); //let resultString = ''; //const instructionPosibilities = [ 'n', 'p', 'm', 'b' ] //for(let i = 1; i <= numBeats; i++){ //const instructionIndex = Math.floor(Math.random()*instructionPosibilities.length); //resultString += instructionPosibilities[instructionIndex] + ' '; //} //console.log(resultString); //5: 3+2, 2+3 //6: 2+2+2, 3+3 //7: 2+2+3, 2+3+2, 3+2+2 //8: 2+3+3, 3+2+3, 3+3+2 //9: 3+3+3, 2+2+2+3, 2+2+3+2, 2+3+2+2, 3+2+2+2 //const breakDown = (num)=>{ //if (num < 2) { //return false; //} else if (num === 2 || num === 3) { //return [num]; //} //const nextNum = num - 2; //const results = [2]; //return results.concat(breakDown(nextNum)) //} const breakDown = (node)=>{ if(node.value > 3 && node.value - 3 > 1){ node.left = { value: 3 } node.right = breakDown({value:node.value-3}) } else if(node.value > 3 && node.value - 2 > 1){ node.left = { value: 2 } node.right = breakDown({value:node.value-2}) } return node; } const tree = breakDown({value:7}); console.log(tree); const getLeaves = (node, leafArray)=>{ if(node.left === undefined && node.right === undefined){ leafArray.push(node.value); } else { getLeaves(node.left, leafArray) getLeaves(node.right, leafArray) } return leafArray; } const leaves = getLeaves(tree, []); console.log(leaves);