Help with Javascript resetting and additions

Hi, I am brand new to this so go easy.

I’m trying to create an aircraft flying hours calculator for a work project, I have got the functionality to add everything and carry across the minutes into extra hours, but I can’t get the sum to run a second time after pressing reset.

this is what I have so far, I’m not fussed about the look and formatting at the moment I’m just trying to get the functionality sorted.

Can anyone help me with this issue and where I’m going wrong?

1 Like

Hello, code pen is saying that you have missing opening divs.

1 Like

The way I use addEventListener is like this:
btnCalculate.addEventListener("click", calculate)
and then I put all the calculation coding into a function called calculate.

This seems to always work.

I think by having the coding inside of the callback and using a parameter inside the callback ‘definition’ causes some behaviours that I haven’t yet fully understood, which is why I code addEventListener as shown above.

1 Like

You are overwriting the references to the elements with the values you get from them inside the handler function.

This is why you shouldn’t use var try replacing your var declarations with const and open the console.

Create different variables for the values inside the function and make sure you declare all your variables (mins and hours are not declared, making them global)


As an aside, forms can be a little broken on Codepen. For example, clicking reset replaces this onClick="window.location.reload();" with this onClick="window.location.removedByCodePen();" (in the DOM).

1 Like

Thankyou for your inputs, I have cracked it!

:smiley:

good luck with your project .