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 trialSean Flanagan
33,235 PointsFahrenheit to Celsius
Hi.
I can't convert correctly, although the final answer isn't far wrong.
Here's my snapshot:
I tried to convert 10 degrees Fahrenheit to Celsius. The final answer should have been -12.2222 but instead it was
Temperature: 10 fahrenheit = -7.7777777777778 celsius
I would be grateful for some help.
Sean
2 Answers
Greg Kaleka
39,021 PointsHi Sean,
Your workspace correctly calculates 10 degrees to -12.222.
Not sure what other code you're using that's getting you the wrong answer, but you would get -7.777 if you did the math all on one line without using parentheses. In your snapshot, you're basically getting the parentheses by default because you're doing the subtraction on its own line first.
This is the correct formula:
C = (F - 32) * 5/9
If you try:
C = F - 32 * 5 / 9
you'll get a completely different answer, because 32 * 5 / 9 will be evaluated first, then F - 17.77778 will be evaluated. That's not what you want.
Make sense?
Math will bite you if you don't watch out
Cheers
-Greg
Sean Flanagan
33,235 PointsHi Greg.
I deleted the conversion variable because I think that over-complicated things. Here's what I have now:
// temperature in fahrenheit
$fahrenheit = 10;
// use the variable above to convert fahrenheit to celsius
$celsius = ($fahrenheit - 32) * 5 / 9;
// display equivalent in celsius
echo "Temperature: ";
echo $fahrenheit;
echo " fahrenheit = ";
echo $celsius;
echo " celsius ";
I then ran the program and got the correct equivalent for 10 degrees Fahrenheit.
I've given your post an up vote and Best Answer. Thanks!
Sean
Greg Kaleka
39,021 PointsPerfect - I agree the conversion variables ($farenheit1, $farenheit2) made the code harder to understand. The teacher did it this way with some of her examples to make it really clear what was happening as she was going through the calculations, but you've done a good refactor .
Nice job!