const numBeats = Math.ceil(Math.random()*9); let resultString = ''; const limbs = [ 'lh', 'rh', 'lf', 'rf' ]; for(let limb of limbs){ let resultString = ''; for(let i = 1; i <= numBeats; i++){ const hit = Math.floor(Math.random()*2); if(hit === 1){ resultString += limb + ' '; } else { resultString += ' '; } } console.log(resultString); } 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:numBeats}); //console.dir(tree, {depth:null}); const getLeaves = (node)=>{ if(node.twoPath === undefined){ return node.value; } let twoPathResult = []; const twoPathLeftValue = getLeaves(node.twoPath.left); const twoPathRightValue = getLeaves(node.twoPath.right); if(Number.isInteger(twoPathRightValue)){ twoPathResult[0] = twoPathLeftValue; twoPathResult[1] = twoPathRightValue; } else { if(Number.isInteger(twoPathRightValue[0])){ twoPathRightValue.unshift(twoPathLeftValue); } else { for(let subArray of twoPathRightValue){ subArray.unshift(twoPathLeftValue); } } twoPathResult = twoPathRightValue; } let threePathResult = []; if(node.threePath !== undefined){ const threePathLeftValue = getLeaves(node.threePath.left); const threePathRightValue = getLeaves(node.threePath.right); if(Number.isInteger(threePathRightValue)){ threePathResult[0] = threePathLeftValue; threePathResult[1] = threePathRightValue; } else { if(Number.isInteger(threePathRightValue[0])){ threePathRightValue.unshift(threePathLeftValue); } else { for(let subArray of threePathRightValue){ subArray.unshift(threePathLeftValue); } } threePathResult = threePathRightValue; } } if(threePathResult.length === 0){ return twoPathResult; } else { if(Number.isInteger(twoPathResult[0])){ if(Number.isInteger(threePathResult[0])){ return [twoPathResult, threePathResult] } else { const result = [twoPathResult]; result.push(threePathResult[0]); return result; } } else { if(Number.isInteger(threePathResult)){ twoPathResult.push(threePathResult) } else { if(Number.isInteger(threePathResult[0])){ twoPathResult.push(threePathResult) } else { twoPathResult = twoPathResult.concat(threePathResult); } } return twoPathResult; } } } const claves = getLeaves(tree); if(Number.isInteger(claves)){ console.log(claves); } else { if(Number.isInteger(claves[0])){ console.log(claves); } else { const clave = claves[Math.floor(Math.random()*claves.length)]; console.log(clave); } }