Anonymous user
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 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.
*/
return balls;
};
Line 1,791 ⟶ 1,807:
const apex = board[1].indexOf(peg);
/**
▲ return (async () => {
*
*/
▲ board[0][apex] = ball;
(async () => {
▲ balls = balls - 1;
while (addBall()) {
iter();
}
await sleep(speed).then(show);
while (iter()) {
await sleep(speed).then(show);
}
await sleep(speed).then(show);
};
{{out}}
<pre>
|