One of two variables, declared the same way, is not working. Whats wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

One of two variables, declared the same way, is not working. Whats wrong?

I'm working on a final submission for one of the web design courses, which involves two variables: adults and children. the adults variable refuses to work, resulting in a reference error. I've spent a dumb amount of time sifting through my HTML, Javascript, even my CSS. Please help!

3rd Jan 2023, 1:35 AM
Benny H
Benny H - avatar
1 Answer
+ 3
Benny H You have misplaced closing brackets in your btn.onclick and window.onload functions...the rest of the code was placed outside. ids are wrong, too. should be "adult" and "child". Get the input elements. I also added a preventDefault to keep the button from submitting the form and refreshing the page. try this: function calc(a, c) { let price = a*12+c*5; return price; } window.onload = function() { let btn = document.getElementById("butt"); btn.onclick = function(e) { let adults = document.getElementById("adult").value; let children = document.getElementById("child").value; e.preventDefault(); if(adults < 0) adults = 0; if(children < 0) children = 0; console.log(adults,children) let price = calc(adults, children); for(let t1 = 1;t1 <= adults;t1++) console.log("Adult Ticket #" + t1); for(let t2 = 1;t2 <= children;t2++) console.log("Children Ticket #" + t2); alert(price); } }
3rd Jan 2023, 2:38 AM
Bob_Li - avatar