As @0stone0 pointed out in the comments, it looks like the action is refreshing the page before calling the function.
Also, it might be necessary to add event.preventDefault() to your calculate() method so it executes and not refreshes the page.
Something like:
function calculate(event){
event.preventDefault();
...
}