diff --git a/random.js b/random.js index 300e58a..f8012ad 100644 --- a/random.js +++ b/random.js @@ -1,12 +1,69 @@ -const numBeats = Math.ceil(Math.random()*9); +//const numBeats = Math.ceil(Math.random()*9); -let resultString = ''; -const instructionPosibilities = [ 'n', 'p', 'm', 'b' ] +//let resultString = ''; +//const instructionPosibilities = [ 'n', 'p', 'm', 'b' ] -for(let i = 1; i <= numBeats; i++){ - const instructionIndex = Math.floor(Math.random()*instructionPosibilities.length); +//for(let i = 1; i <= numBeats; i++){ + //const instructionIndex = Math.floor(Math.random()*instructionPosibilities.length); - resultString += instructionPosibilities[instructionIndex] + ' '; + //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 = (num)=>{ + const results = []; + + let firstResult = num - 3; + if(firstResult > 3){ + firstResult = breakDown(firstResult); + } + const firstArray = [3, firstResult]; + results.push(firstArray); + + + let secondResult = num - 2; + if(secondResult > 3){ + secondResult = breakDown(secondResult); + } + const secondArray = [2, secondResult]; + results.push(secondArray); + + + return results; } +const result = breakDown(7); +console.dir(result, {depth:null}); -console.log(resultString); +const traverse = (tree)=>{ + let result = ''; + for(let node of tree){ + if(Number.isInteger(node)){ + result += node + ', '; + } else { + result += traverse(node); + } + } + return result; +} +const flattenedTree = traverse(result); +console.log(flattenedTree);