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

iOS

How do I solve the Fahrenheit to Celcius challenge?

Hello,

After completing the "Operators" part of the "Swift basics" course, a bonus challenge is presented:

"Using the math operators taught in this stage create a formula to change any given temperature from Fahrenheit to Celsius."

I've tried to solve this in Swift by writing this code:

let celcius = 20 let fahrenheit = (1.8 * celcius) + 32

(I don't know how to properly include code examples)

It's doesn't work, and I think it has something to do with the order of which the code is being "calculated" (The multiplication is not being applied).

How do I solve this challenge? :)

Thanks in advance,

Nicklas

6 Answers

You have to change "celsius" to a float number like this :

let celsius = 20.0

let fahrenheit = (1.8 * celsius) + 32

println(celsius)

I did it! Thank you!

The code looks like this:

let celcius: Float = 20.0
var fahrenheit: Float; (1.8 * celcius) + 32

try this formula :

let fahrenheit = 20 as Int
let celsius = (fahrenheit-32)*5/9 as Int
println(celsius)

I still can't get it to work. What do you mean by "as Int"? Like in:

let fahrenheit: int = 20?

I've tried to convert the other way around (since I am Danish): http://imgur.com/mRpMZ01

It still does not work. Would be happy to learn from it just by getting the result and a short explaination.

Thank you!

int means integer and float means decimal point number

let fahrenheit = 32

let celsiusConversion = Double(fahrenheit - 32) * 5 / 9

// The answer will be calculated be changing the value/temperature assigned to constant fahrenheit. Remember we are asked to convert from fahrenheit to celsius.

Here's my version:

import UIKit

var currentTemperature = 45.0

let celsiusMultiplier = 1.8

var Celsius = (currentTemperature - 32) / celciusMultiplier

print(celsius)

My version Formula Fahrenheit to Celsius F = (C x 2) + 30

let Celsius1 = 10.0 let Fahrenheit1 = (Celsius1 * 2) + 30 let fahrenheitLetter = "F" println("(Fahrenheit1)(fahrenheitLetter)")

Formula Celsius to Fahrenheit C = (F-32) X 5/9

let Fahrenheit2 = 50.0 let Celsius2 = (Fahrenheit2-32) * 5/9 let celsiusletter = "C" println("(Celsius2)(celsiusletter)")

Extra Credit: Using the math operators taught in this stage create a formula to change any given temperature from Fahrenheit to Celsius. Formula for conversion: T(°C) = (T(°F) - 32) / 1.8

var fTemp = 72
var cTemp = Double(fTemp - 32) / 1.8
println(cTemp)