I'm a student who is relatively fairly new to JS. Unfortunately I'm quite stuck on this issue, and no amount of googling has helped me regarding this.
I've omitted most of the irrelevant HTML side of things, however this is my code. Essentially this is a program which can allow a user to choose components for a PC and estimate a price for it.
var tPrice = 0
function Pick(obj) {
Comp = ["p3", "p5", "p7", "16GB", "32GB", "1TB", "2TB", "19", "23", "MNT", "MDT", "2P", "4P"];
Price = [100, 120, 200, 75, 150, 50, 100, 65, 120, 40, 70, 10, 20];
Cart = [];
PriceCart = [];
var value = obj.value;
var cIndex = Comp.indexOf(value);
var cPrice = Price[cIndex];
tPrice = (tPrice + cPrice);
document.getElementById("dtPrice").innerHTML = ("$" + tPrice);
Cart.push(value);
PriceCart.push(cPrice);
for (var i = 0; i < Comp.length; i++) {
var sList = Cart[i] + " $"
PriceCart[i];
}
document.getElementById("dsList").innerHTML = sList;
}
<div class="sidebar">
<h3>Shopping Cart :</h3>
<p id="dsList">You have bought nothing!</p>
<h3>Total Price</h3>
<p id="dtPrice">$0</p>
</div>
My issue as of now is that the program perfectly works without the for loop, however after I add it in, the entire function stops working.
As for what the for loop specifically does, it is supposed to print two separate arrays together as an active 'shopping cart'.
What exactly have I done with the for loop that breaks the entire program?
+beforePriceCart[i].sList, overwriting the work of the previous iteration.