If/else statement game

Each time the arithmetic question is generated and whenever i pick the right or wrong answer the statement of function checkanswer() always goes on if-statement “Correct”. How am i able to create if/else statement correctly base on an arithmetic question varanswer = var1 + var2

 function checkAnswer(event) if(questionGen(varAnswer) \> 1 ) then instructionsText.text = "Correct!"; instructionsText:toFront() generateBalls() for i=1, allBallsGroup.numChildren do display.remove(allBallsGroup[1]) end else instructionsText.text = "Incorrect!"; instructionsText:toFront() generateBalls() for i=1, allBallsGroup.numChildren do display.remove(allBallsGroup[1]) end end end function questionGen() local questionVar1 = display.newImage("ygSquare.png", 150, 500); local var1 = math.random(1,9) local var1Display =display.newText(var1, 200, 500, native.systemFont, 200) questionVar1.width = 200 questionVar1.height = 200 questionVar1.x = 350 questionVar1.y = 500 var1Display.x = 350 var1Display.y = 500 var1Display:setTextColor("#000000") local questionVar2 = display.newImage("blueSquare.png", 150, 500); local var2 = math.random(1,9) local var2Display = display.newText(var2, 200, 500, native.systemFont, 200) questionVar2.width = 200 questionVar2.height = 200 questionVar2.x = 700 questionVar2.y = 500 var2Display.x = 700 var2Display.y = 500 var2Display:setTextColor("#000000") local operator = " + " local operatorDisplay = display.newText(operator, 400, 500, native.systemFont, 200) operatorDisplay:setTextColor("#000000") local varAnswer = var1 + var2 return varAnswer end

i don’t know what #theAnswer is but it tells you the number or rows in array

example:

local myTable = {1,“hello”, true}

print(#myTable) – prints 3


local myTable = {1,“hello”}

print(#myTable) – prints 2

Line 3…

You are passing varAnswer into a function that does not accept a parameter.

questionGen is always returning a result greater than 1, so you are always getting the “Correct” answer no matter what the value is.

I also do not see a variable that stores the user’s guess.

I think you want something like this…

if questionGen() == myGuess then

Not sure why you have that “> 1” check, since any guess greater than 1 will be successful, and that not what you want.

You need a variable that holds the guess that the user enters and you compare that value to the result that is returned from questionGen().  It looks like you are calling questionGen, then displaying the math question, so I would do something like this:

-- This displays the question and holds the answer for later answer = questionGen() -- Once the user enters an answer, store that into another variable (myAnswer for example), -- and then you compare it if (myAnswer == answer) then print ("Correct!") else print ("Incorrect") end

From what I am reading, you want the user to enter an answer to a math problem, and then compare the user answer to the correct answer.  Is that correct?

If so, you don’t compare anything to “>1”.  the code I provided above will compare the user answer to the match question.  Extract the value from your event listener and compare that to the correct answer.

If this is not what you are trying to do, then my apologies, and I would need you to explain.

i don’t know what #theAnswer is but it tells you the number or rows in array

example:

local myTable = {1,“hello”, true}

print(#myTable) – prints 3


local myTable = {1,“hello”}

print(#myTable) – prints 2

Line 3…

You are passing varAnswer into a function that does not accept a parameter.

questionGen is always returning a result greater than 1, so you are always getting the “Correct” answer no matter what the value is.

I also do not see a variable that stores the user’s guess.

I think you want something like this…

if questionGen() == myGuess then

Not sure why you have that “> 1” check, since any guess greater than 1 will be successful, and that not what you want.

You need a variable that holds the guess that the user enters and you compare that value to the result that is returned from questionGen().  It looks like you are calling questionGen, then displaying the math question, so I would do something like this:

-- This displays the question and holds the answer for later answer = questionGen() -- Once the user enters an answer, store that into another variable (myAnswer for example), -- and then you compare it if (myAnswer == answer) then print ("Correct!") else print ("Incorrect") end

From what I am reading, you want the user to enter an answer to a math problem, and then compare the user answer to the correct answer.  Is that correct?

If so, you don’t compare anything to “>1”.  the code I provided above will compare the user answer to the match question.  Extract the value from your event listener and compare that to the correct answer.

If this is not what you are trying to do, then my apologies, and I would need you to explain.