Here's the modified JavaScript code with the "salada" option removed and some improvements:
<html>
<head>
<link rel="icon" href="http://www.iconarchive.com/download/i87143/graphicloads/colorful-long-shadow/Restaurant.ico">
<title>O 100 Saladas</title>
</head>
<body>
<center>
<h1>O 100 Saladas</h1>
<img src="restaurante.jpg" border="1" width="400" height="400" name="pratoImage"/>
<br><br>
<label for="pratoSelect">Escolha um prato:</label>
<select id="pratoSelect">
<option value="">-- Selecione --</option>
<option value="carne">Carne</option>
<option value="peixe">Peixe</option>
<option value="vegetariano">Vegetariano</option>
</select>
<br><br>
<label for="acompanhamentos">Acompanhamentos:</label>
<input type="checkbox" id="arrozCheckbox" name="acompanhamentos" value="arroz">
<label for="arrozCheckbox">Arroz</label>
<input type="checkbox" id="batatasCheckbox" name="acompanhamentos" value="batatas">
<label for="batatasCheckbox">Batatas</label>
<br><br>
<button type="button" onclick="resetForm()">Reset</button>
<button type="button" onclick="fazerPedido()">Fazer pedido</button>
</center>
<script>
const imagePaths = {
carne: {
arroz: "carnea.jpg",
batatas: "carneb.jpg",
arrozBatatas: "carneab.jpg",
default: "carne.jpg",
},
peixe: {
arroz: "peixea.jpg",
batatas: "peixeb.jpg",
arrozBatatas: "peixeab.jpg",
default: "peixe.jpg",
},
vegetariano: {
arroz: "vegeta.jpg",
batatas: "vegetb.jpg",
arrozBatatas: "vegetab.jpg",
default: "vegetariano.jpg",
}
};
const pratoSelect = document.getElementById("pratoSelect");
const arrozCheckbox = document.getElementById("arrozCheckbox");
const batatasCheckbox = document.getElementById("batatasCheckbox");
const pratoImage = document.getElementsByName("pratoImage")[0];
function resetForm() {
pratoSelect.selectedIndex = 0;
arrozCheckbox.checked = false;
batatasCheckbox.checked = false;
pratoImage.src = imagePaths.carne.default;
}
function fazerPedido() {
const prato = pratoSelect.value;
const arroz = arrozCheckbox.checked;
const batatas = batatasCheckbox.checked;
if (prato) {
let imagePath = imagePaths[prato].default;
if (arroz && batatas) {
imagePath = imagePaths[prato].arrozBatatas;
} else if (arroz) {
imagePath = imagePaths[prato].arroz;
} else if (batatas) {
imagePath = imagePaths[prato].batatas;
}
pratoImage.src = imagePath;
}
}
</script>
</body>
</html>
Improvements made:
-
Simplified the HTML structure by removing unnecessary tags and attributes.
-
Refactored the JavaScript code to use an object (imagePaths) to store the paths for the images, instead of using multiple Image objects.
-
Simplified the logic for selecting the image path based on the options selected by the user.
-
Made the resetForm() function more robust by resetting all form components to their default state.