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

if you sold 47 items for 9.99 but only paid 5.45 for each item, how much money did you make

I can't figure this out.

script.js
var wholesalePrice = 5.45;
var retailPrice = 9.99;
var quantity = 47;
var salesTotal = '9.99 ' * '47';  
var profit =  'salesTotal ' - ('wholesalePrice ' * 'quanity');
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

Jacob Herrington
Jacob Herrington
15,835 Points

You are on the right track, but you've got some syntax errors and it seems like you might be misunderstanding a couple of these concepts.

Your syntax errors are here:

var salesTotal = '9.99 ' * '47';  
var profit =  'salesTotal ' - ('wholesalePrice ' * 'quanity');

In JavaScript, quotes (' or ") are only necessary when you are referring to a string, which is something like a sentence, word, or character. e.g.

var stringExample = 'This is a string';
var otherString = "Also a string"; // double quotes and single quotes both denote a string
var otherOtherString = 'a';
var lastString = '12345'; // JavaScript sees this as non-numbers because I put it in quotes

You've put numeric data types into quotes by defining salesTotal as '9.99' * '47' and the challenge didn't see the answer it expected as a result. By removing those quotes, JavaScript will understand that we wanted 9.99 and 47 to be treated like numbers, not a string. But please understand, the challenge is asking for you to use the variables provided, not numbers you've hard-coded in yourself.

The same goes for when referring to a variable:

var aVariable = 123;
var wrong = 'aVariable'; //JavaScript sees the string "aVariable"
var right = aVariable; //JavaScript sees the value stored in the var named aVariable (123)

See if you can figure out where you've placed unnecessary quotes in the other parts of your code.

If you need help, here is passing code for the task, make sure you understand this before you move on though, or you will have a lot of trouble:

var wholesalePrice = 5.45;
var retailPrice = 9.99;
var quantity = 47;
var salesTotal = retailPrice * quantity;  
var profit = salesTotal - (wholesalePrice * quantity);

When I struggle with concepts like this, I typically try re-watching the video at a slower speed with closed-captions enabled.

Cheo R
Cheo R
37,150 Points

Your're trying to multiply and subtract string representations.

var salesTotal = '9.99 ' * '47';  
var profit =  'salesTotal ' - ('wholesalePrice ' * 'quanity');

Change '9.99' to 9.99 (or just replace with retailPrice) and '47' to 47. Since salesTotal, wholePrice, and quantity area already variables, there's no need to put them into strings.