parent
25674725d5
commit
64d5bddff4
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue