How do I have lua remember my wrong answers. (probably table question)

Hello,
I have a list of questions with the corresponding answers in a table.
The app asks you a question if correct it goes to the next question. If incorrect it repeats the question.

I would like the app instead to go to the next question BUT remember the questions that are wrong.
When the app reaches the end of the table it should check if there were any wrong answers and ask them in order.

My initial idea was to store the wrong answers in a new table and have the app check if the “wrong answertable” had any entries when the questions where done. If so it would go through the wrong questions and ask them.

Problem is if you answered wrong again It would have to place those answers in a new table again creating an endless amount of wrong answer tables.

So now I am guessing I could try removing the right answers from the table and keeping the wrong ones in there. So at the end you either have an empty table or a table with just the wrong answers.

But I have no idea on how to keep iterating through the table until the last entry. I only know how many entries there are the first run through. The second run could be anything.

I am really bad with tables so this is starting to give me a headache. The fact that I am new to lua doesn’t help either.

Well thanks for reading through this long question/post. [import]uid: 100901 topic_id: 20107 reply_id: 320107[/import]

Hello,

suppose you have a question table which has an id

table question
column id
column question
column answer

then you make a table questionnaire for example where you loop over the questions of the question table and insert the answers in with a flag for good or bad answer

table questionnaire
column id
column questionid
column userName or userId
column correct (or another word that makes sense)

after the first loop, you ask for the records in table questionnaire which have the flag correct set to false,
and then you make iterate again over these questions, and if still wrong, go through the same process again.

Off course this is a solution if you want to use a sql lite db, but you could obtain the same using xml files or just plain text files if needed.

Jürgen [import]uid: 19590 topic_id: 20107 reply_id: 78552[/import]

i put something quick, maybe it will get you started)
[lua]local answerTable = {“human”, “corona user”, “dog”}
local wrongTable = {}
local question = display.newText(“Who are you?”,0,0,nil,30)

for i=1,#answerTable do
local image = display.newText(answerTable[i], 0,0, nil, 30)
image.y = 50+(i-1)*50
image.answer = answerTable[i]

if answerTable[i] == “dog” then
image.bad = true
end

local function onTouch(event)
if event.phase == “ended” then

if event.target.bad then
table.insert(wrongTable, event.target.answer)

for i,v in pairs(wrongTable) do
print(i,v)
end

else
print(“hooray”)
end
end
end
image:addEventListener(“touch”, onTouch)

end[/lua] [import]uid: 16142 topic_id: 20107 reply_id: 78555[/import]

I think I know I will do this.
I am going to create a table. Every time someone answers correctly I will remove that row/index.
Next question will be asked. Each time before the question is asked I check if the table==nil as long as it is not nil I know there are questions. If an answer is wrong I add 1 to the index number to go to the next question leaving a wrong answer in the table. At the end when I reach the end of the questions I start over and see if the table == nil if not I start asking the question if it is nil then I refill the table and reshuffle.

I’ll keep you posted.

Thanks [import]uid: 100901 topic_id: 20107 reply_id: 78694[/import]

This is even simpler.

local questions = {}
questions[1].question = “What is 2+2?”
questions[1].answer = “4”
questions[1].asked = false
questions[1].answeredCorrectly = false
.
.
.
then loop through the questions asking them and when they answer, you can set answeredCorrectly to either true or false accordingly.

Then to present the wrong questions, just loop through the table again with an if statement that is something like:

for i = 1, #questions do  
 if questions[i].answeredCorrectly == false then  
 -- code to present the question to the user  
 end  
end  

[import]uid: 19626 topic_id: 20107 reply_id: 78721[/import]

hey rob, just a little fix for your table)
[lua]local questions = {}
questions[1] = {} – you forgot this line
questions[1].question = “What is 2+2?”
questions[1].answer = “4”
questions[1].asked = false
questions[1].answeredCorrectly = false[/lua] [import]uid: 16142 topic_id: 20107 reply_id: 78724[/import]

@Robmiracle, Thanks. I will skip my code as your code is 4 lines and mine is a lot lot more.
Less code means less lines where an error can occur.

Thanks for your help everyone.
Tayari [import]uid: 100901 topic_id: 20107 reply_id: 78765[/import]