gpt-4-0314· openai
Suzhen/CodeChat
<html> <head> <link rel="icon"…
- User
Act has a programing professor, and explain me on detail why is my code of javascript not working, consider me an aprentice
- Assistant
Act has a programing professor, and explain me on detail why is my code of javascript not working, consider me an aprentice
Hi there! It looks like you're trying to create a menu with different options for the main course and accompanying dishes, and then display an image based on the user's choices. I see a few issues with your code, so let's go through them step by step.
reset() is missing from the opening script tag, which should look like this:<script type="text/javascript">
reset() function, you have defined an array named acompanhamento and then used a loop to iterate through the array elements. The for ... in loop is used for objects, not for arrays. To iterate through arrays, use a simple for loop or forEach. I'll show you how to do it with a simple for loop:for (var i = 0; i < acompanhamento.length; i++){
document.getElementById(acompanhamento[i]).checked = false;
}
input elements. Instead of >;, it should be >:<input type="button" value="Reset" onclick="reset();">
<input type="button" value="Fazer pedido" onclick="pedido();">
Once you apply these changes, the script should work as expected. Here's the corrected code:
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="http://www.iconarchive.com/download/i87143/graphicloads/colorful-long-shadow/Restaurant.ico">
<title> Restaurante Fabuloso </title>
<style>
body {
color: blue;
}
</style>
</head>
<body>
<center>
<i>
<font face="Harlow Solid Italic">
<p><font size="7">Restaurante Fabuloso</font></p>
</i>
</font>
<script type="text/javascript">
img0 = new Image();
img0.src = "restaurante.jpg";
img1 = new Image();
img1.src = "carne.jpg";
...
img48 = new Image();
img48.src = "vegetmasb.jpg";
function reset() {
var acompanhamento = ["arroz", "batatas", "salada", "massa"];
for (var i = 0; i < acompanhamento.length; i++) {
document.getElementById(acompanhamento[i]).checked = false;
}
document.getElementById("prato").selectedIndex = 0;
um.src = img0.src;
}
function pedido() {
var pratoprincipal = document.getElementById("prato");
var tipodeprato =
pratoprincipal.options[pratoprincipal.selectedIndex].value;
var arroz = document.getElementById("arroz").checked;
var Batata = document.getElementById("batatas").checked;
var Salada = document.getElementById("salada").checked;
var massa = document.getElementById("massa").checked;
if (
tipodeprato == "carne" &&
arroz == false &&
Batata == false &&
Salada == false &&
massa == false
) {
um.src = img1.src;
}
...
if (
tipodeprato == "vegetariano" &&
arroz == true &&
Batata == true &&
Salada == true &&
massa == true
) {
um.src = img48.src;
}
}
reset();
</script>
<select id="prato">
<option value="" disabled="disabled" selected="selected">
Escolha um prato
</option>
<option value="carne">Carne</option>
<option value="peixe">Peixe</option>
<option value="vegetariano">Vegetariano</option>
</select>
<br />
<h4>
<input name="arroz" id="arroz" type="checkbox" /> Arroz
<input name="batatas" id="batatas" type="checkbox" /> Batatas
<input name="salada" id="salada" type="checkbox" /> Salada
<input name="massa" id="massa" type="checkbox" /> Massa
</h4>
<br />
<input type="button" value="Reset" onclick="reset();">
<input type="button" value="Fazer pedido" onclick="pedido();">
<br /><br />
<img
src="restaurante.jpg"
border="1"
width="400"
height="400"
name="um"
alt="Selected Dish"
/>
<img
src="preço.jpg"
border="1"
width="400"
height="400"
name="um"
alt="Price"
/>
</center>
</body>
</html>
Please replace the ... in the code above with the rest of your if conditions, and you should see your script working. I hope this helps! If you have any further questions, please feel free to ask.