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 trialJonathan Jackson
2,716 PointsJavaScript calculates from left to right. This question can't be completed without another variable, wholeSaleCost.
I'm being asked to write code that calculates profit gained.
var wholeSalePrice = 5.45; var retailPrice = 9.99; var quantity = 47;
var salesTotal = retailPrice * quantity;
This so far gives the total retail sale, but the question wants me to find the profit gained apparently without creating another variable, and seeing as though JavaScript doesn't follow Order of Operations, I don't think that's possible.
The only way I see to solve it is by doing this:
var wholeSalePrice = 5.45; var retailPrice = 9.99; var quantity = 47;
var costTotal = wholeSalePrice * quantity; var salesTotal = retailPrice * quantity;
var profit = salesTotal - costTotal;
But TeamTreehouse wants me to not use a new variable so I can't do that. I just don't see how the question is answerable (without making a really long and pointless line of code)
2 Answers
Julian Aramburu
11,368 PointsHi Jonathan! Javascript math works like anyother math! Just do:
var profit = salesTotal - (wholesalePrice * quantity);
Caleb Kleveter
Treehouse Moderator 37,862 PointsWell the instructions say "Create another variable named profit. It should hold the value of the salesTotal variable minus the wholesalePrice multiplied by the quantity. In other words, if you sold 47 items for 9.99 but only paid 5.45 for each item, how much money did you make?", So they do want you to make another variable. Did I miss something?
Julian Aramburu
11,368 PointsHe's creating a new variable called costTotal which is not asked in the task at hand.
Jonathan Jackson
2,716 PointsJonathan Jackson
2,716 PointsI could've sworn it moved from left to right instead of by order of operations.