-
Notifications
You must be signed in to change notification settings - Fork 0
/
JS
34 lines (30 loc) · 943 Bytes
/
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
document.addEventListener('DOMContentLoaded', function() {
const spots = document.getElementsByClassName('spot');
// Set up event listeners for parking and leaving buttons
document.getElementById('park-btn').addEventListener('click', function() {
parkCar();
});
document.getElementById('leave-btn').addEventListener('click', function() {
leaveCar();
});
function parkCar() {
for (let i = 0; i < spots.length; i++) {
if (spots[i].classList.contains('empty')) {
spots[i].classList.remove('empty');
alert('Car parked in spot ' + (i + 1));
return;
}
}
alert('No available spots.');
}
function leaveCar() {
for (let i = 0; i < spots.length; i++) {
if (!spots[i].classList.contains('empty')) {
spots[i].classList.add('empty');
alert('Car left from spot ' + (i + 1));
return;
}
}
alert('No cars to leave.');
}
});