Skip to content
Advertisement

how to edit jquery calculation start from zero

I want my code to start from zero at the total odds and the payout amount, the button with 9.00 is supposed to append the div to the cart and remove when i re-click on it

the problem occurs when i re-click on the button the calculation remains the same, It is supposed to go back to zero as in there is nothing in the cart

how do i get it to calculate until zero?

here is the model of my calculation:

JavaScript
JavaScript

Advertisement

Answer

To find out where is the problem you should look into browser dev console. There you might notice that you are getting a similar error, when you click on 9.00 button second time:

JavaScript

And what it actually means is that you can not do a values.reduce when your values are empty.

JavaScript

How you can fix it ?

For example, you can add an additional check in your code that checks if values are empty then consider total to be 0.

JavaScript

Or you can still using reduce, but then provide a second parameter (initial value) to reduce function:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement