Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCHARLES REROMA
771 PointsWhen using ( ) in computations...
So I'm working on a challenge on this path and I got an error, basically I wanted to compute two values first thats why I placed them inside the ( ) but then I got a wrong answer. So my question is,
What is the difference between the two below?
var wholesalePrice = 5.45; var retailPrice = 9.99; var quantity = 47; var salesTotal = retailPrice * quantity; var profit = salesTotal - wholesalePrice * quantity;
var wholesalePrice = 5.45; var retailPrice = 9.99; var quantity = 47; var salesTotal = retailPrice * quantity; var profit = (salesTotal - wholesalePrice) * quantity;
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! Let's take a look a closer look at the computations. This is all based on the order of operations that you probably learned in math class in middle school sometime. But, many of us don't have to use it on a day to day basis so it gets easily forgotten. I was always taught the phrase "Please Excuse My Dear Aunt Sally" to help remember them. Parentheses, exponents, multiplication, division, addition, and subtraction. Parentheses are done first. Exponents are done second. Multiplication and division come 3rd, and addition and subtraction come 4th. If two operations are on the same "level" of importance (multiplication/division and addition/subtraction) then they are done from left to right.
salesTotal in both examples is calculated to be 469.53
In your first example (substituting the values in for the variables):
- Profit equals wholesalePrice times quantity subtracted from salesTotal
- wholesalePrice times quantity is equal to 256.15
- profit is then equal to 469.53 - 256.15
- profit is equal to 213.38
In your second example:
- First, we subtract wholesalePrice from salesTotal
- The result is 469.53 - 5.45
- This gives us 464.08
- Then we multiply that by the quantity of 47
- 464.08 * 47
- Profit would then equal 21,118.76
As you can see, the order of operations can greatly skew the results.
Hope this helps clarify things!
Aiden Campbell
6,618 PointsIt should work, I pasted your comment into my chrome console and I was able to get a number for profit so it looks like it works, make sure you are entering it correctly on whatever text editor you are using.