I am making a script that allows me to perform operations to generate the total cost of the products:
var subtotal = 0;
var ivaValor = 0;
var total = 0;
selected.forEach(function(dato) {
var pretotal = dato.price * dato.quantity;
parseFloat(pretotal)
var total = total + pretotal;
});
console.log(total);
But total returns it to me as Not a number (NaN), at the moment I don't understand what happens since total is being declared int.
Note: I'm just starting out in JS :)
I couldn't get it to show me NaN but I did notice that you are declaring 2 variables
total
, one outside the foreach and one inside:Because of the last line I gather that you want to add the value of the operations
dato.price * dato.quantity;
to the variabletotal
inside theforeach
. Remove the declaration of the internal total variable fromforEach
for the operation to work: