Page 249 - Web_Application_v2.0_C12_Fb
P. 249
}
li {
margin: 10px 0;
cursor: pointer;
color: blue;
text-decoration: underline;
}
</STYLE>
</HEAD>
<BODY>
<H1>Car List Menu</H1>
<UL>
<LI onclick="addCar()">1. Add a car to the list</LI>
<LI onclick="removeLastCar()">2. Remove the last car</LI>
<LI onclick="deleteCarByName()">3. Delete a specific car by name</LI>
<LI onclick="showTotalCars()">4. Show total number of cars</LI>
<LI onclick="showAllCars()">5. Show all car names</LI>
<LI onclick="exitProgram()">6. Exit</LI>
</UL>
<SCRIPT>
let carList = [];
function addCar() {
let carName = prompt("Enter the name of the car to add:");
if (carName) {
carList.push(carName);
alert(carName + " has been added to the list.");
} else {
alert("Car name cannot be empty.");
}
}
function removeLastCar() {
if (carList.length > 0) {
let removedCar = carList.pop();
alert(removedCar + " has been removed from the list.");
} else {
alert("The list is already empty.");
}}
function deleteCarByName() {
if (carList.length > 0) {
let carToDelete = prompt("Enter the name of the car to delete:");
let index = carList.indexOf(carToDelete);
JavaScript Part 2 247

