I have two arrays. They look like this:
array price = 14.60, 39.00
and
array quantity = 10, 5
(quantity is the quantity of items the user want to buy - 10 items from productA and 5 of productB)
Now I want loop through the variables to multiply the price with the quantity.
Like :
14,60 * 10
and
39,00 * 5
and add the two results to the endPrice variable.
I get the quantity array like this:
$('.quantity').change(function () {
quantitys = $('input[type=number]').map(function () {
return $(this).val();
}).get();
});
and the different prices like this:
var prices = $('.priceCell').map(function () {
return parseFloat($(this).html().trim());
}).get();
And that's what I tried:
var endPrice = 0;
for (var q = 0; q < quantitys.length; q++) {
for (var p = 0; p < prices.length; p++) {
endPrice = quantitys[q] * prices[p];
}
}
alert(endPrice);
Well, that haven't worked for me so well. Can someone help me there? Doesn't matter if the solution is pure JavaScript or jQuery.
quantitysandpricesare as you expect them to be?Uncaught SyntaxError: Unexpected identifier) since neither of those assignments declare, or initialise, an Array. To do so they should look like:price = [14.60, 39.00];Note the surrounding square brackets.