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 trialAkshat Jain
6,810 PointsWhat is wrong with the following code?
The challenge fails and says it setting the wrong value
class Temperature {
var celsius: Float = 0.0
var fahrenheit: Float {
get {
return (celsius * 1.8) + 32.0
}
set {
celsius = (fahrenheit - 32) / 1.8
}
}
}
5 Answers
Dave Pinchoff
4,440 PointsAkshat -
Inside your set code you have this written:
celsius = (fahrenheit - 32) / 1.8
Instead, you should have this written:
celsius = (newValue - 32) / 1.8
because this is a code that instructs the program to change the value of celsius if there is ever a "newValue" given for farenheit. Is that clear? :-)
Dave Pinchoff
4,440 PointsHi there -
In the setter code, you shouldn't be using"fahrenheit" there. You need to use the property "newValue".
Hope this helps!
Akshat Jain
6,810 PointsHmmn, I don't understand. How?
tytyty
4,974 PointsOh my Fn God. This got me too. After coming all this way, this was the only challenge that got me. However I don't blame myself. LOL No where in these lesson examples was (newValue) placed inside calculations. I didn't even know you could do that. Bad lesson challenge IMHO. Or they should've had a lesson example where (newValue) was placed inside some sort of calculation. I think this would stump a lot of 'true' noobs.
Dave Pinchoff
4,440 PointsIt is in the lesson example to use newValue - go back and rewatch that video, specifically when he types in the Setter code. Trust me.. it took me a bit, but it's there!!! :-)
tytyty
4,974 PointsI didn't say it's NOT in the video. Read carefully! I said there is not an example of placing "(newValue)" within a calculation/equation.
Want me to prove it??? Here's Amit's code example.
set {
length = sqrt(newValue)
width = sqrt(newValue)
}
Teaching this example without a calculation doesn't make it clear/known to me that it can be used in a calculation since I've never touched code before. I just assumed it was something to be used on its own.
Yes now it's clear because 20/20 ;)
Akshat Jain
6,810 PointsAkshat Jain
6,810 PointsAh, I see