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

Nathan F.
30,773 PointsImproving upon the random functions in Fun Facts
I decided to improve upon the Fun Fact app. You might've noticed that it's possible for the color, fact, or (rarely) both to stay the same as before when tapping the "Show Another Fun Fact" button because of the random function picking from the array without consideration of what was selected before.
This is a pretty ordinary problem, and I've solved it before. And I did manage to solve it for the color backgrounds, so far as I can tell. But sometimes facts still display twice in a row. I fear I'm missing something very simple, but wanted to share what I have and get another pair of eyes. The relevant code bits are in my ViewController.swift file:
@IBAction func showFunFact(sender: AnyObject) {
var randomColor = colorWheel.randomColor()
// before setting new colors, check to see if background color
// is already the same as the newly assigned color, and if so
// call the function again for another color.
if (view.backgroundColor == randomColor) {
// keep trying until the background color and random color
// are different. Will it work? Or is this going to cause a nasty crash?
while view.backgroundColor == randomColor {
randomColor = colorWheel.randomColor()
}
}
view.backgroundColor = randomColor
funFactButton.tintColor = randomColor
var randomFact = factBook.randomFact()
if (funFactLabel.text == randomFact) {
while randomFact == funFactLabel.text {
randomFact = factBook.randomFact()
}
} else {
funFactLabel.text = randomFact
}
}
So, what is it I'm missing here? I set a variable that gets the string returned by the randomFact() method. Then I look at the variable and compare it to funFactLabel's text property. If they match, I want to keep calling the function and re-assigning the variable until they no longer match.
This doesn't work. Why?
1 Answer

Nathan F.
30,773 PointsOkay, I feel tremendously silly. Can you see my mistake? Because I came back to it, started adding println statements to everything to step through the flow of things, and the problem leapt out to me, clear as day, before I even got a chance to run the thing through the console.
It's quite simple--the while loop keeps running until the text label and the randomFact variable no longer match, but it never actually sets a new value on the textLabel outside of the while loop.
Arrrrgh! Well, at least it's solved. I can sleep easy again.