Owen
Owen

Reputation: 13

Javascript Uncaught ReferenceError: cannot assign to function call

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
    }
}

Upvotes: 0

Views: 1460

Answers (1)

connexo
connexo

Reputation: 56720

Replace

document.getElementById(`${items[item]}-cost`) = sum;

with

document.getElementById(`${items[item]}-cost`).textContent = sum;

You cannot assign something to what a function call returns; you can only assign to variables and object properties.


Not related, but also wrong:

Replace

for (let item in items)

which is meant to be used to iterate over own properties of an object, with

for (let item of items)

which is meant to iterate over an array (which is what you are trying).

Upvotes: 1

Related Questions