From 64d5bddff4dc0cd02584bb2cafe437192e9a27c8 Mon Sep 17 00:00:00 2001 From: Matthew Huntington Date: Tue, 2 May 2023 13:23:30 -0400 Subject: [PATCH] drums.js --- random.js => drums.js | 0 sax.js | 115 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) rename random.js => drums.js (100%) create mode 100644 sax.js diff --git a/random.js b/drums.js similarity index 100% rename from random.js rename to drums.js diff --git a/sax.js b/sax.js new file mode 100644 index 0000000..e6d07f1 --- /dev/null +++ b/sax.js @@ -0,0 +1,115 @@ +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); + +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); + } +}