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: Doing Math Code Challenge, What's Wrong With My Code??

Hey guys,

So I'm doing JavaScript Basics with Dave McFarland and I have hit a snag that I just can't get my head around.

I'm not sure if there is a bug with the code challenge or if my code is actually incorrect.

The Question

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?

My Answer

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

I look over the code and the question and my answer makes sense to me, apparently not to the code challenge.

I go ahead and click on "Check Work" and then I get that annoying "Oops! It looks like Task 1 is no longer passing." message.

As mentioned earlier, this code makes sense to me and I feel that it should be working, but I have been wrong wrong with saying this before, may need a second set of eyes.

Thanks in advanced

Stu : )

Hope this will work

var profit = ( retailPrice - wholesalePrice ) * quantity

also check case sensitive in wholesalePrice

14 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Stu;

Not sure that it matters, but it looks like you changed the variable wholesalePrice to wholeSalePrice.

Here is my code through Task 2:

var wholesalePrice = 5.45;
var retailPrice = 9.99;
var quantity = 47;

//Task 1
var salesTotal = retailPrice * quantity;

//Task2
var profit = salesTotal - (wholesalePrice * quantity);

Looks like the code is good with that change. Hard to tell exactly what the Challenge Checker is looking at sometimes.

Ken

Thanks for this Ken Alger, as mentioned to Dave, I must've changed the variable name when I was half asleep haha.

Cheers

Stu : )

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Stu;

You need to use parenthesis in your math formula.

If you recall 100 - 5 * 6 is mathematically different from 100 - (5 * 6).

Hope it helps.

Ken

Try this:

var profit = salesTotal - (wholeSalePrice * quantity);

Hey Ladna,

This unfortunately does not work. Thanks for your help anyway.

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Stu Cowley

JavaScript is very picky -- variables are case sensitive: the variable wholesalePrice is not the same as the variable wholeSalePrice. Check out your code and I think you will see what I mean.

Ken Alger
Ken Alger
Treehouse Teacher

Stu;

I missed that earlier too, that's why it is great to have teachers like Dave McFarland around, even on a holiday!

Ken

Thanks for that Dave McFarland that makes perfect sense, don't know why I didn't see that earlier, I'll have a crack at this later on, and I'll let you know how I go : ).

Thanks for having a look at this for me, especially seeing that it's Thanksgiving!

Nice! I could have sworn there wasn't any case error. I practically checked like 5 times. Oh well, thanks Dave

I got the exact same problem as Stu had. At first, I was quite certain I did not change the variable name in line 1, it just seemed to have changed just by clicking the "Next task" button. However, when I encountered the error message about task 1 failing, I may have changed the variable name without really thinking about it.

I have tried redoing these tasks several times to see if I could recreate the condition that caused the error, and it seems likely that I have actually changed the variable name. But, there is still one problem with the challenge. Having passed task 1, and just declaring the profit variable in task 2, will cause task 1 to fail. Going back to task one with the exact same code, and task 1 will again pass.

I think there is something odd in the code checker, since task 1 should not fail as long at the salesTotal variable is calculated correctly. Or am I wrong? It's quite late here, just as it was for Stu :)

Ok guys,

So I have had a crack once again at this challenge, but I am still getting that annoying "Oops! It looks like Task 1 is no longer passing." message coming up when I click on "Check Work"

Here is the code I am using with the help you've all given me.

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

No doubt I am missing something dumb once again.

Stu : )

Dave McFarland
Dave McFarland
Treehouse Teacher

Sorry about that Stu. It looks like you renamed the original wholesalePrice variable to wholeSalePrice in the first line of the code. I think the Code Challenge is analyzing your code and expecting the original variable name wholesalePrice (with a small s) not with the capital S you changed it to.

You basically have the correct answer -- just the variable name needs to match what the challenge is looking for:

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

Sorry it's so picky.

Hey Dave McFarland

Thanks for that, I'm not sure why I changed the variable name to be honest, it was quite late when I was doing it so that might explain it, haha.

Just wanted to quickly say thanks for the way that you have done your videos in JavaScript Basics. I have never really been able to get a good grasp on the language but now I am finding that I am : )

Cheers,

Stu :)

Dave McFarland
Dave McFarland
Treehouse Teacher

Good to hear Stu Cowley! Thanks for the feedback.

I just ran into trouble with this task. At first I tried this line of code:

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

Basically I have written it out in the same way I would work it out on a calculator. Could somebody please explain why the above is different to:

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

To me, these two calculations have the same result. Am i missing something here?

Thanks in advance for any help and thanks to Dave for making this course, I think you've done a great job.

Jonathan Wogenius
Jonathan Wogenius
1,981 Points

I think the question is formated in a way that many people makes a misstake here, the correct answer is:

var wholesalePrice = 5.45;
var retailPrice = 9.99;
var quantity = 47;
var salesTotal = retailPrice * quantity;
var profit = salesTotal - (wholesalePrice *quantity);
Dave McFarland
Dave McFarland
Treehouse Teacher

Hi Jonathan -- interesting point. Do you have a suggestion for way to format the question so it's less confusing?

Jonathan Wogenius
Jonathan Wogenius
1,981 Points

"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 I aded the comma. Hehe. Well my native language is Swedish so it might be confusing for me. But I somehow misunderstand it like: var profit = (salesTotal - wholesalePrice) * quantity;

However it might just be me and my Swedish. And of course! Thanks for the best online courses available! :)

Just got it, wholesale, is one work, so it should be entered as "wholesale". Cool :)

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

var profit = 9.99 * 47 - 5.45 * 47; this one works.

Complete the variable declaration by adding the keyword that does not allow the value stored in price to change:

var retailPrice = 9.99

Is not the correct answer. It the one below that I just discovered.

____ price = 9.99

const is the variable name.