I'm working on this snippet that calculates the cost of buying multiple items at a time. Every time the user buys 1 item, the price increases by 15%.
I'm getting this error: Uncaught ReferenceError: cannot assign to function call when trying to display the total to the document.
Is it where I have converted the .toFixed() value to a number (function) so the sum is equal to a function value? How could I get around this?
function displayStore() {
// Testing on 1 item
const items = ['paw'];
// If not buying 1
if (game.storeMultiple !== 1) {
for (let item in items) {
// set p to initial price
let p = storeItems[items[item]].cost;
let sum = p;
for (let n = 1; n < game.storeMultiple; n++) {
// set p to new price
p = Number((p * 1.15).toFixed(1));
// add to sum
sum += p;
}
console.log(`${items[item]}-cost`); // logs 'paw-cost'
console.log(sum); // logs 203
document.getElementById(`${items[item]}-cost`) = sum; // Uncaught ReferenceError: cannot assign to function call
}
}
document.getElementByIdis a function call and returns an html node, you can't assign something to it. Get the property you want to modify on the node (textContent or innerHTML, etc..) and assign you value to this.