funkyhippo
funkyhippo

Reputation: 17

Expected expression, got '}', can't find error anywhere

Very basic JS user, learning for a class right now so ignore any optimizations to the code or anything that could be made. Part of the assignment is creating this button that calculates a final cost given variables. Upon clicking the button, FireFox gives me an error: "expected expression, got '}'" I don't see any open brackets or other syntax errors, any help?

<button id="totalSales" onclick = "
                totalCost = (totalCost + burgerOnePrice*Number(burgerOne.value));
                totalCost = (totalCost + burgerTwoPrice*Number(burgerTwo.value));
                totalCost = (totlCost + burgerThreePrice*Number(burgerThree.value));
                totalCost = totalCost*(1+tip);

                    if (useCard==1) {

                        if (Number(balance.value) >= totalCost) {
                            totalCost = 0;
                            cardBalance = cardBalance-totalCost;
                            balance.value = cardBalance;
                            finalCost.value = totalCost;

                        } else {
                        totalCost = (totalCost-Number(balance.value));
                            balance.value = "$0.00";
                            finalCost.value = totalCost;
                        }

                    } else if (useCard==0) {
                        totalCost = totalCost;
                        finalCost.value = totalCost;
                    }


                 " >Calculate Total</button> 
                 <br>

        <input type="text" id="finalCost" value="" size="3" readonly="true" />

Upvotes: 0

Views: 92

Answers (3)

Ali Behbudov
Ali Behbudov

Reputation: 1

You should not write your javascript code into your HTML tag.

First, create js function in script tag

    function myFunction() {
  totalCost = (totalCost + burgerOnePrice * Number(burgerOne.value));
  totalCost = (totalCost + burgerTwoPrice * Number(burgerTwo.value));
  totalCost = (totlCost + burgerThreePrice * Number(burgerThree.value));
  totalCost = totalCost * (1 + tip);

  if (useCard == 1) {

    if (Number(balance.value) >= totalCost) {
      totalCost = 0;
      cardBalance = cardBalance - totalCost;
      balance.value = cardBalance;
      finalCost.value = totalCost;

    } else {
      totalCost = (totalCost - Number(balance.value));
      balance.value = $0.00;
      finalCost.value = totalCost;
    }

  } else if (useCard == 0) {
    totalCost = totalCost;
    finalCost.value = totalCost;
  }

}

then use html onclick event parameter like that.

<button onclick="myFunction();" id="totalSales">Calculate Total</button>

Upvotes: 0

edu
edu

Reputation: 153

In this line balance.value = "$0.00"; you are using double quotes "
This makes the browser think the onclick ended there, and from then on, it just can't make heads or tails of anything.

But you shouldn't be writing that much code inside html, I would even say that you shouldn't even use onclick inside html elements.

It is a much better idea to use the addEventListener method in a separate .js file.

Upvotes: 1

user229044
user229044

Reputation: 239402

The first time you use " in your JavaScript, you're ending the onclick="..." attribute, leading to a syntax error.

You shouldn't be cramming that much JavaScript into your DOM. Put it in a function, and call the function from your event handler.

Upvotes: 5

Related Questions