what do I have to change to this code, so that it doesn't take "balls" with the value of 10 every time? More precisely, "balls" to keep their last value. Thank you in advance!
let balls = 10;
let randomB = Math.floor(Math.random() * 10) + 1;
if (randomB % 2 == 0) {
balls++;
} else {
balls -= 2;
}
console.log(balls)
Documentation says:
Functions are one of the fundamental building blocks in JavaScript. A function in JavaScript is... a set of statements that performs a task or calculates a value...
Documentation says:
The current context of execution... The context in which values and expressions are "visible" or can be referenced... Scopes can also be layered in a hierarchy...
class Game {
constructor(balls) {
this.balls = balls;
}
step() {
let randomB = Math.floor(Math.random() * 10) + 1;
if (randomB % 2 == 0) {
this.balls++;
} else {
this.balls -= 2;
}
return this.balls;
}
}
const game = new Game(10);
do {
stepResult = game.step();
console.log(stepResult);
} while (stepResult != 0);
So:
step.