Galton box animation: Difference between revisions

m
Comments and cleanup
m (Comments and cleanup)
Line 1,701:
Works with NodeJs
<lang JavaScript>const readline = require('readline');
 
const galtonBox2 = (layers, balls) => {
/**
* Galton Box animation
* @param {number} layers The number of layers in the board
* @param {number} balls The number of balls to pass through
*/
const galtonBox2galtonBox = (layers, balls) => {
const speed = 100;
const ball = 'o';
Line 1,748 ⟶ 1,754:
/**
* Collect the result.
* @param {number} idx
*/
const appendToResult = idx => {
Line 1,783 ⟶ 1,789:
});
return hasNext;
};
 
/**
* Add a ball to the board.
* @returns {number} The number of balls left to add.
*/
returnconst (asyncaddBall = () => {
board[0][apex] = ball;
balls = balls - 1;
return balls;
};
 
Line 1,791 ⟶ 1,807:
const apex = board[1].indexOf(peg);
 
/**
return (async () => {
* whileRun (balls)the {animation
*/
board[0][apex] = ball;
(async () => {
balls = balls - 1;
while (addBall()) {
await sleep(speed);
showawait sleep(speed).then(show);
iter();
}
await sleep(speed).then(show);
show();
while (iter()) {
await sleep(speed).then(show);
show();
}
await sleep(speed).then(show);
show})();
})().then(() => console.log('Done!'));
 
 
};
 
galtonBox2galtonBox(12, 50);</lang>
</lang>
{{out}}
<pre>
Anonymous user