Trying to make a test application. Literally.

How can I populate a list of a b c d answers being populated directly from the question array?



I want a random quesiton to populate and put radio buttons for a b c d below with answers then check the answer.


Can I put everything inside an array and check strings?


Or is there a better method and logic that I need to comprehend?


I was thinking about using switch statements somehow?


Does anyone here know what terry A davis was talking about when he was talking about radic comparision in this interview?


I was thinking that would almost be a more efficient way to compare data, but have absolutley no idea if the comparasin method even exists.


THanks for your time.


Shalom.

Accepted Reply

This spec is really not clear, so I'll ask a few questions.


How can I populate a list of a b c d answers being populated directly from the question array?

I want a random quesiton to populate and put radio buttons for a b c d below with answers then check the answer.


1. So, you want random question from a list of questions (the question array) ?

2. You want to display 4 answers a, b, c, d. One will be correct, the other 3 not.

3. How do you want to select those other 3? Do you predefine them ? Or do you want to select them randomly ? (however they need to have some logical link to the question).


Can I put everything inside an array and check strings?

Or is there a better method and logic that I need to comprehend?


You need first to build your data models.

I would suggest create a dictionary for Q&A, as explained in the other thread:


struct QA {
   var question: String
   var correctAnswer: String
   var phonytAnswers: [String] = ["", "", ""]     // The ones to use in buttons, along with the correct one
}

then

var allQA: [QA]


That you populate with your questions.

let qa1 = QA(question: "Who is Steve Jobs", correctAnswer: "Apple's Founder", phonytAnswers: ["Labor minister in the 80's", "Former US President", "Nobel Prize"])
let qa2 = QA(question: "What is the capital of France", answer: "Paris", phonytAnswers: ["Chicago", "Toulouse", "Lyon"])
let qa3 = QA(question: "Who was the first man on the moon", answer: "Neil Armstrong", phonytAnswers: ["Barak Obama", "Buzz Aldrin", "John Glenn"]
"Buzz Aldrin", "Jogn Glenn"])
allQA = [qa1, qa2, qa3]allQA = [qa1, qa2, qa3

So your func to get a question and its answers is:

func getQuestion() -> QA {
     return allQA.randomElement()!
}

Then, you display with:

let question = getQuestion().question


you get all the answers with

let correct answer = getQuestion().correctAnswer

let phonytAnswers = getQuestion().phonytAnswers


Create an array [UIButton] to hold the 4 buttons.

Set the tags of the buttons from 0 to 3

Each time you select a question:

- select a random number let correctNum = (0...3).random

- set the title of the button number correctNum with the correct answer

- set the other titles with the phony answers

- in the IBAction (the same for all buttons), just test if tag == correctNum

- if so, answer is correct, otrherwise it is wrong.

Replies

This spec is really not clear, so I'll ask a few questions.


How can I populate a list of a b c d answers being populated directly from the question array?

I want a random quesiton to populate and put radio buttons for a b c d below with answers then check the answer.


1. So, you want random question from a list of questions (the question array) ?

2. You want to display 4 answers a, b, c, d. One will be correct, the other 3 not.

3. How do you want to select those other 3? Do you predefine them ? Or do you want to select them randomly ? (however they need to have some logical link to the question).


Can I put everything inside an array and check strings?

Or is there a better method and logic that I need to comprehend?


You need first to build your data models.

I would suggest create a dictionary for Q&A, as explained in the other thread:


struct QA {
   var question: String
   var correctAnswer: String
   var phonytAnswers: [String] = ["", "", ""]     // The ones to use in buttons, along with the correct one
}

then

var allQA: [QA]


That you populate with your questions.

let qa1 = QA(question: "Who is Steve Jobs", correctAnswer: "Apple's Founder", phonytAnswers: ["Labor minister in the 80's", "Former US President", "Nobel Prize"])
let qa2 = QA(question: "What is the capital of France", answer: "Paris", phonytAnswers: ["Chicago", "Toulouse", "Lyon"])
let qa3 = QA(question: "Who was the first man on the moon", answer: "Neil Armstrong", phonytAnswers: ["Barak Obama", "Buzz Aldrin", "John Glenn"]
"Buzz Aldrin", "Jogn Glenn"])
allQA = [qa1, qa2, qa3]allQA = [qa1, qa2, qa3

So your func to get a question and its answers is:

func getQuestion() -> QA {
     return allQA.randomElement()!
}

Then, you display with:

let question = getQuestion().question


you get all the answers with

let correct answer = getQuestion().correctAnswer

let phonytAnswers = getQuestion().phonytAnswers


Create an array [UIButton] to hold the 4 buttons.

Set the tags of the buttons from 0 to 3

Each time you select a question:

- select a random number let correctNum = (0...3).random

- set the title of the button number correctNum with the correct answer

- set the other titles with the phony answers

- in the IBAction (the same for all buttons), just test if tag == correctNum

- if so, answer is correct, otrherwise it is wrong.

Claude can you help me through my logical thinking process... this is what I see


  1. struct QA { (I am building a struct called QA that will hold data)
  2. var question: String (question will be a string)
  3. var correctAnswer: String (answer will be a string)
  4. var phonytAnswers: [String] = ["", "", ""] (SO IS THIS an Array or wrong answer // The ones to use in buttons, along with the correct one
  5. }

var allQA: [QA] (OK HERE IS WHERE I AM GETTING LOST----- We just made a variable allQA which is an array? That holds Question, Correct Answer, phony answers?]


I really get lost past this concept.... I know you are right in your approach not quesitoning that as I am a pea compared to you, but I just don't know what we really created there....


I am going to run the dump(allQA) to see what prints out i guess.


Thanks for hangin with me man. I am really enjoying this process of learning to program. Shalom.

import UIKit


struct QA {

var question: String

var correctAnswer: String

}


var allQA: [QA]


let q1 = QA(question:"In the preparaton of a rental agreement, the landlord and tenant can agree to include terms prohibited by law?", correctAnswer: "False" )

let q2 = QA(question:"Unless the rental agreement says differently, the tenant shal pay fair market rent for the use of the dwelling unit as determined by the landlord.", correctAnswer: "True")


allQA = [q1,q2]


func getQuestion() -> QA(


return allQA.randomElement()

)



I AM GETTING 2 ERRORS ON THE FUNC



1. Consecutive statements on a line must be separated by ';'


2. Expected expression in list of expressions


Thanks for helping me get the struct down correctly....coming together...shalom

In what I proposed, I assumed your game (like on some TV game) was to ask a question and not only ask if true or false, but propose 4 answers with just one correct and ask user to tell which.


struct QA {  // This is a struct which hols all the information for a question: 
     var question: String // the question iself, will be a string)
     var correctAnswer: String // the correct answer, will be a string)
     var phonyAnswers: [String] = ["", "", ""]   // Yes, SO IS THIS an Array or wrong answer  // The ones to use in buttons, along with the correct one
}


Now you really build the game by creating the QA for the game.

You store all the QA in an array

var allQA: [QA] // YES. We just made a variable allQA which is an array? That holds Question, Correct Answer, phony answers?]


And you fill the array with your own set of questions.


I really get lost past this concept.... I know you are right in your approach not quesitoning that as I am a pea compared to you, but I just don't know what we really created there....

Now in the game, you select randomly a QA.

You displmay its question in a label

Below, you can display four buttons labelled A, B, C, D,

and close to each button, one of the 4 answers (correct and phony).

Of course, you would select where the correct answer will be (A, B, C, D) randomly and put the other answers in the other positions.

To keep track of correct answer, you can set the tag of the button for the correct answer to 1, and others to 2, 3, 4


And you ask user to tap the correct button answer.

If the button tapped has tag of 1, it is the correct answer.

Just congratulate


You can add to the game, by letting user use its credit to eliminate 2 wrong answers for instance…

Now, uip to you to imagine the complete game.

OK, that is a simùpler game, may be easier to start with.


func getQuestion() -> QA(
    return allQA.randomElement()
)

The error is because content of func should be in curly braces, not parenthesis.


Change to


func getQuestion() -> QA {
    return allQA.randomElement()
}

Ok Claude hang with me



import UIKit


struct QA {

var question: String

var correctAnswer: String

}


(Created a struct which can have a long list of variables)....check





var allQA: [QA]





(Created array to hold all QA struct variables) Check





let q1 = QA(question:"In the preparaton of a rental agreement, the landlord and tenant can agree to include terms prohibited by law?", correctAnswer: "False" )

let q2 = QA(question:"Unless the rental agreement says differently, the tenant shal pay fair market rent for the use of the dwelling unit as determined by the landlord.", correctAnswer: "True")



(Creating unchangable arrays which hold differing QA structs)....not sure why created the allQA:[QA] could we have just went to creating questions then put them in an array like the next step?



allQA = [q1,q2]


OK I think I just figured out we just intialize a variable holding a struct which is an array in the step above....we are now placing the variables inside the arrary...think i got that now....





func getQuestion() -> QA {


return allQA.randomElement() ??


} Expected expression after operator



Now we are creating a function called getQuestion(). getQuestion() returns a QA struct



inside the fuction it will pic a random element insided the allQA array.....why does it require a () at the end and ?? two question marks...starting to make a bit more sense though. Thanks for your help.


and also i get an Expected expression after operator error on the last line?

Now i need a way to compare the strings from the true and false radio buttons and add a counter. could you help me out. Thanks.

You should create a new post for this question, and format the code as here below with the <> tool.


import UIKit

struct QA {
    var question: String
    var correctAnswer: String
}

//  (Created a struct which can have a long list of variables)....check

var allQA: [QA]

// (Created array to hold all QA struct variables) Check

let q1 =  QA(question:"In the preparaton of a rental agreement, the landlord and tenant can agree to include terms prohibited by law?", correctAnswer: "False" )
let q2 = QA(question:"Unless the rental agreement says differently, the tenant shal pay fair market rent for the use of the dwelling unit as determined by the landlord.", correctAnswer: "True")

//  (Creating unchangable arrays which hold differing QA structs)....not sure why created the allQA:[QA] could we have just went to creating questions then put them in an array like the next step?

allQA = [q1,q2]

//  OK I think I just figured out we just intialize a variable holding a struct which is an array in the step above....we are now placing the variables inside the arrary...think i got that now....

func getQuestion() -> QA {

    return allQA.randomElement() ??

}      //  Expected expression after operato

Now we are creating a function called getQuestion(). getQuestion() returns a QA struct


inside the fuction it will pic a random element insided the allQA array.....why does it require a () at the end and ?? two question marks...starting to make a bit more sense though. Thanks for your help.


and also i get an Expected expression after operator error on the last line?


For this error: Expected expression after operator:

allQA.randomElement returns an optional.

So, you should either unwrap or use ?? with a value, such as q1


func getQuestion() -> QA {
    return allQA.randomElement() ?? q1
}

or


func getQuestion() -> QA {
    return allQA.randomElement()!
}



Now i need a way to compare the strings from the true and false radio buttons and add a counter.


As you ask only for ture or false, you could change the struc to keep track if true or falset:


struct QA {
    var question: String
    var correctAnswer: Bool
}
let q1 =  QA(question:"In the preparaton of a rental agreement, the landlord and tenant can agree to include terms prohibited by law?", correctAnswer: false)
let q2 = QA(question:"Unless the rental agreement says differently, the tenant shal pay fair market rent for the use of the dwelling unit as determined by the landlord.", correctAnswer: true)


And so, you know immediately how to score.


For the counter, just declare

var totalAnswers = 0
var correctAnswers = 0


You have 2 radioButtons: trueButton, falseButton

I suppose the current qustion is loaded as:

let currentQA = getQuestion()


In the IBAction for the buttons

totalAnswers += 1


in trueButton IBAction:

if currentQA.correctAnswer { correctAnswers += 1 }


in falseButton IBAction:

if !currentQA.correctAnswer { correctAnswers += 1 }

thanks..I will get back to you when I work through this. Toda shalom.