Okay, so I've been doing some JS practice and I decided to make a basketball simulation in console. The main issue for me is making a timings between functions:
var Ascore = 0;
var Bscore = 0;
var time = 10;
var ball = 0;
var rng = 0;
function timer() {
time--;
if (time >0) {
start();
setTimeout(timer, 1000);
console.log(time);
} else {
console.log('Game is over!');
console.log(teamAscore + ':' + teamBscore)
}
}
timer();
function start() {
rng = Math.random()*100;
if (rng < 50) {
aball();
console.log('Team A gets the ball!');
} else {
bball();
console.log('Team B gets the ball!');
}
}
function aball() {
rng = Math.random()*100;
if (rng > 50) {
Ascore + 2;
console.log('Team A scored 2 points!');
bball();
} else {
console.log('Team A missed!');
rng = Math.random()*100;
if (rng > 50) {
aball();
console.log('Team A rebounded!');
} else {
bball();
console.log('Team B got the rebound!');
}
}
}
function bball() {
rng = Math.random()*100;
if (rng > 50) {
Bscore + 2;
console.log('Team B scored 2 points!');
aball();
} else {
console.log('Team B missed!');
rng = Math.random()*100;
if (rng > 50) {
bball();
console.log('Team B rebounded!');
} else {
aball();
console.log('Team A got the rebound!');
}
}
}
I pasted the code since people can't understand it. Right now everything works BUT it keeps going over and over and over infinitely. I want 5 second delay betwen each bball, aball function.