-
Notifications
You must be signed in to change notification settings - Fork 0
/
roulette.js
48 lines (40 loc) · 1.65 KB
/
roulette.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let balance = 1000;
function playRoulette() {
const betAmount = parseFloat(document.getElementById('betAmount').value);
const betNumber = parseInt(document.getElementById('betNumber').value);
const resultDiv = document.getElementById('result');
const rouletteNumbersDiv = document.getElementById('rouletteNumbers');
const balanceSpan = document.getElementById('balance');
if (isNaN(betAmount) || isNaN(betNumber) || betNumber < 0 || betNumber > 36) {
resultDiv.innerHTML = "Please enter a valid bet amount and number.";
return;
}
if (betAmount > balance) {
resultDiv.innerHTML = "Insufficient balance.";
return;
}
const winningNumber = Math.floor(Math.random() * 37);
let currentNumber = 0;
let count = 0;
const maxCount = 20;
const interval = setInterval(() => {
currentNumber = Math.floor(Math.random() * 37);
rouletteNumbersDiv.innerHTML = currentNumber;
count++;
if (count >= maxCount) {
clearInterval(interval);
rouletteNumbersDiv.innerHTML = winningNumber;
let resultMessage = "";
if (betNumber === winningNumber) {
const winnings = betAmount * 35;
balance += winnings;
resultMessage = `Congratulations! You have won: ${winnings} $`;
} else {
balance -= betAmount;
resultMessage = `You have lost. Your Bet Amount: ${betAmount} $`;
}
balanceSpan.innerHTML = balance;
resultDiv.innerHTML = resultMessage;
}
}, 100);
}