You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.4 KiB

//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 = (node)=>{
if(node.value < 4){
return node
}
//2 path
node.twoPath = {};
node.twoPath.left = { value: 2 }
node.twoPath.right = breakDown({value:node.value-2})
//3 path
if(node.value > 4){
node.threePath = {}
node.threePath.left = { value: 3 }
node.threePath.right = breakDown({value:node.value-3})
}
return node;
}
const tree = breakDown({value:5});
console.dir(tree, {depth:null});
const getLeaves = (node, leafArray)=>{
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]);
if(node.threePath !== undefined){
const leftValue = getLeaves(node.threePath.left, leafArray)
const rightValue = getLeaves(node.threePath.right, leafArray)
leafArray.push([leftValue, rightValue]);
}
}
const leaves = []
getLeaves(tree, leaves);
console.log(leaves);