+ 1
Some guide me to fix errors in this code
let adult =12; let children=6; let price = (adult+children) console .log(price); for(let i=1; i<=adult; i++){ console .log("Ticket#" + "num") } function calc(adult ,children ){ return("price"); } calc(price); window.onload = function() { let btn = document.getElementById("BuyButton"); btn.onclick = function() { let adults = document.getElementById("adults").value; let children = document.getElementById("children").value; let price = calc(adults, children); alert(price); } }
3 Answers
+ 3
Here's your code with the errors fixed and some improvements:
```javascript
let adult = 12;
let children = 6;
let price = adult + children;
console.log(price);
for (let i = 1; i <= adult; i++) {
console.log("Ticket#" + i);
}
function calc(adult, children) {
return adult + children; // Calculate the total price instead of returning the string "price"
}
window.onload = function () {
let btn = document.getElementById("BuyButton");
btn.onclick = function () {
let adults = parseInt(document.getElementById("adults").value); // Parse the input values as integers
let children = parseInt(document.getElementById("children").value);
let totalPrice = calc(adults, children); // Calculate the total price
alert("Total Price: quot; + totalPrice); // Display the total price
};
};
```
Improvements made:
1. Added missing variable declaration for `i` in the loop.
2. Changed the loop to display the correct ticket number `i`.
3. Fixed the `calc` function to calculate the total
+ 1
this should work: let adult = 12;
let children = 6;
let price = adult + children;
console.log(price);
for (let i = 1; i <= adult; i++) {
console.log("Ticket#" + i);
}
function calc(adult, children) {
return adult + children;
}
calc(price);
window.onload = function () {
let btn = document.getElementById("BuyButton");
btn.onclick = function () {
let adults = parseInt(document.getElementById("adults").value);
let children = parseInt(document.getElementById("children").value);
let price = calc(adults, children);
alert(price);
};
};
+ 1
There's an unknown error in line 18 this out and verify check out