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 trialJack Campbell
1,822 PointsMaking the app orderly instead of random
I would like to make the app orderly instead of random. Meaning my random facts would be put in order instead of being randomly selected. Does anyone know how to do this or have a resource that could be helpful?
4 Answers
Jack Campbell
1,822 PointsHi Aaron,
Thank you so much for your answer. I am relatively new to coding so they may sound stupid! But would I insert this code into factbook.m? If so under which line of code would I put this under? Thanks so much!
agreatdaytocode
24,757 PointsHi Jack,
You can use an array to pull that off. Start at index 0. Then increase the number each time a questions is displayed. Let me know if you need an example.
Jack Campbell
1,822 PointsHey Aaron,
An example would be awesome, thanks so much!
agreatdaytocode
24,757 PointsI'm sure there is a better way of doing this in your code. That said copy this into a playground file and modify it as you need.
Basically the function "nextQuestion" takes an array of questions and then returns the question based on the counter. Each time it runs it increases the counter by 1. It also check to make sure the counter is never above the array count. Let me know if you have any more questions.
let questions = ["question 1", "question 2","question 3","question 4","question 5"]
var counter = 0
func nextQuestion(quetions: Array<String>) -> String {
if counter == questions.count {
counter = 0
}
return questions[counter]
}
nextQuestion(quetions: questions)
counter += 1
nextQuestion(quetions: questions)
counter += 1
nextQuestion(quetions: questions)
counter += 1
nextQuestion(quetions: questions)
counter += 1
nextQuestion(quetions: questions)
counter += 1
nextQuestion(quetions: questions)
counter += 1
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsI just noticed this was an Objective-c problem and not Swift. That said, can I ask why you are learning Obj-c?
Jack Campbell
1,822 PointsJack Campbell
1,822 PointsTrying to get an app built in a short amount of time and the end product of this app is similar to what I am wanting to build.