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 trial

JavaScript JavaScript Basics (Retired) Working With Numbers Doing Math

Jonathan Jackson
Jonathan Jackson
2,716 Points

JavaScript 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
Julian Aramburu
11,368 Points

Hi Jonathan! Javascript math works like anyother math! Just do:

var profit = salesTotal - (wholesalePrice * quantity);
Jonathan Jackson
Jonathan Jackson
2,716 Points

I could've sworn it moved from left to right instead of by order of operations.

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Well 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
Julian Aramburu
11,368 Points

He's creating a new variable called costTotal which is not asked in the task at hand.