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.

64 lines
1.5 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)=>{
if(node.twoPath === undefined){
return node.value;
}
const twoPathLeftValue = getLeaves(node.twoPath.left);
const twoPathRightValue = getLeaves(node.twoPath.right);
if(node.threePath !== undefined){
const threePathLeftValue = getLeaves(node.threePath.left);
const threePathRightValue = getLeaves(node.threePath.right);
return [[twoPathLeftValue, twoPathRightValue], [threePathLeftValue, threePathRightValue]];
}
return [twoPathLeftValue, twoPathRightValue];
}
const leaves = getLeaves(tree);
console.dir(leaves, {depth:null});