Debugging a function

Here is a snippet from my code:


function convert(suit,value) -- converts the suits and values to be drawn onto card
    if value=="Jack" then
        local v = {symbol=***,abbrev="J"}
    elseif value=="Queen" then
        local v = {symobol=***,abbrev="Q"}
    elseif value=="King" then
        local v = {symbol=***,abbrev="K"}
    elseif value=="Ace" then
        local v = {symbol=suit,abbrev="A"}
    else
        local v = {symbol=suit,abbrev=value}
    end

    if suit=="Diamonds" then
        local v = ***
    elseif suit=="Hearts" then
        local v = ***
    elseif suit=="Spades" then
        local v =  ***
    elseif suit=="Clubs" then
        local s = ***
    end

    return {s=s,v=v}
end

And this is in my setup function:

a=convert("Diamonds","Jack")
print(a.s)

This prints nil, why is that and how can I fix it?

Also, emojis can’t be posted, so I just replaced them with asterisks, replace them with emojis when reviewing.

@Doge Wrong answer, I didn’t read the code correctly.

@Doge - probably

  1. because three out of four of your suit tests are using v instead of s #-o

  2. you define v and s as local within an if test, they won’t exist outside it. Rather initialise local v,s=0,0 at the top of the function.

@Doge Try this.


function setup()
    a=convert("Diamonds","Jack")
    print(a.s)
    print(a.v.symbol)
    print(a.v.abbrev)
end


function convert(suit,value) -- converts the suits and values to be drawn onto card
    local v,s
    if value=="Jack" then
        v = {symbol="***",abbrev="J"}
    elseif value=="Queen" then
        v = {symobol="***",abbrev="Q"}
    elseif value=="King" then
        v = {symbol="***",abbrev="K"}
    elseif value=="Ace" then
        v = {symbol=suit,abbrev="A"}
    else
        v = {symbol=suit,abbrev=value}
    end

    if suit=="Diamonds" then
        s = "***"
    elseif suit=="Hearts" then
        s = "***"
    elseif suit=="Spades" then
        s =  "***"
    elseif suit=="Clubs" then
        s = "***"
    end

    return {s=s,v=v}
end

@Ignatz, Oh my god I have no idea how I missed that ~X(
@dave1707 Thanks for the example :slight_smile: