Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Working With Numbers Doing Math

I don't get what I'm getting wrong

I don't get what I'm doing wrong any pointers.

script.js
var wholesalePrice = 5.45;
var retailPrice = 9.99;
var quantity = 47;
var salesTotal = retailPrice * quantity;
var profit = saleTotal - wholesalePrices * quantity ;
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

2 Answers

Cheryl Oliver
Cheryl Oliver
21,676 Points

You need to put brackets around wholesale and quantity. That is because you need to multiply wholesalePrice and quantity BEFORE you subtract. If you dont put brackets around it will subtract and then multiply.

var wholesalePrice = 5.45;

var retailPrice = 9.99;

var quantity = 47;

var salesTotal = retailPrice * quantity;

var profit = salesTotal - (wholesalePrice * quantity);

Steven Parker
Steven Parker
220,378 Points

Actually, the parentheses are not necessary.

Like most languages, JavaScript has rules for "operator precedence". Since multiplication (*) takes precedence over subtraction (-), it will be performed first even if it comes later in the expression.

But with that said, it can also be good practice to use parentheses just to make your intentions clear, or to make a complicated expression more readable, even when they are not necessary to get the correct result.

See this MDN page for more information on operator precedence.

Steven Parker
Steven Parker
220,378 Points

You have a couple of spelling errors:

  • you wrote "saleTotal" (singular) instead of "salesTotal"
  • you wrote "wholesalePrices" (plural) instead of "wholesalePrice"