Comparing integer variables in LUA

I am getting a very strange error when trying to compare 2 integer variables using LUA on the Corona SDK.

This is basically what I have

**jAnswer** -- is a variable set via jSON, the value can only be 0 or 1.

    local function checkAnswer(answer)

       if (answer == jAnswer ) then
          print("Correct Answer")
          print("Answer is = "..answer.." jAnswer = "..jAnswer)
       else 
          print("Wrong Answer")
          print("Answer is = "..answer.." jAnswer = "..jAnswer)
       end

    end

checkAnswer(1) -- Calling the Function Here

      

The problem is, even if you get an output like "Answer is = 1, jAnswer = 1", I still get "Invalid answer".

+3


source to share


1 answer


Basically, jAnswer was viewed as a String, so I just needed to convert the string to a number using a global class in lua named tonumber()

jAnswer = tonumber(jAnswer, 10) -- Convert using the decimal base

      



Thank!

+4


source







All Articles