0

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
    }
}
2
  • document.getElementById is 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. Commented Oct 12, 2022 at 18:24
  • 1
    Ugghg.... of course. Just one of those moments haha. Commented Oct 12, 2022 at 18:29

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Great thanks for the extra info :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.