Is there a neater/shorter way to write this code?

I’ve been using lua for a month but stopped using it for a while. When I came back I made this simple code:

-- Remember, in a linear graph, the function is y = mx + b find the intercept-- m = 2 b = 4 function slopeFinder() print("y = mx + b") print("y = ".. m .."x + ".. b) print("Slope is ".. m ..", y-intercept is "..b) function graphDir() if m \> 0 then return(" Since the slope is ".. m.. ", the graph is positve. The direction will go up") end if m \< 0 then return(" Since the slope is ".. m.. ", the graph is negative. The direction will go down") end if m == 0 then return(" Since the slope is ".. m.. ", the graph is constant. The direction is straight or horizontal") end end function linearChk() if m ~= 0 then return(" The graph is linear") else return(" The graph is not linear") end end return (" The slope is ".. m.. " and the y-intercept is ".. b.. ".".. graphDir() .. ".".. linearChk() .. ".") ; end return slopeFinder();

Its pretty long (36 lines of code) and messy. It took me almost an hour trying to rework it after there were syntax errors. So is there a way to reduce the amount of lines in this code? I just hate having 36 lines just for a basic code that just returns 3 lines of words.

How about something like this function? It’s not vastly shorter (in lines) but it’s a bit cleaner.

[lua]

local function slopeFinder( m, b )

    – Remember, in a linear graph, the function is y = mx + b find the intercept-- 

    print( “y = mx + b” )

    print( "y = "… m …"x + "…  b )

    print( "Slope is “… m …”, y-intercept is "…b )

    local graphDirResult = “(unknown)”

    local linearChkResult = “(unknown)”

    local function graphDir()

        if m > 0 then 

            graphDirResult = " Since the slope is "… m… “, the graph is positve. The direction will go up”

        elseif m < 0 then 

            graphDirResult = " Since the slope is "… m… “, the graph is negative. The direction will go down”

        elseif m == 0 then 

            graphDirResult = " Since the slope is "… m… “, the graph is constant. The direction is straight or horizontal”

        end

    end

    function linearChk()

        if m ~= 0 then

            linearChkResult = " The graph is linear"

        else

            linearChkResult = " The graph is not linear"

        end

    end

    graphDir()

    linearChk()

    return " The slope is "… m… " and the y-intercept is "… b… “.”… graphDirResult … “.”… linearChkResult … “.”

end

[/lua]

Then, call the function like this:

[lua]

local slopeFinderResult = slopeFinder( 2, 4 )

print( slopeFinderResult )

[/lua]

Best regards,

Brent

Thanks Brent. I’ll reconsider that next time.

local function slopeFinder( m, b ) -- Remember, in a linear graph, the function is y = mx + b find the intercept-- print( "y = mx + b" ) print( "y = ".. m .."x + ".. b ) print( "Slope is ".. m ..", y-intercept is "..b ) local graphDirResult = "(unknown)" local linearChkResult = "(unknown)" if m \> 0 then graphDirResult = " Since the slope is ".. m.. ", the graph is positve. The direction will go up" elseif m \< 0 then graphDirResult = " Since the slope is ".. m.. ", the graph is negative. The direction will go down" elseif m == 0 then graphDirResult = " Since the slope is ".. m.. ", the graph is constant. The direction is straight or horizontal" linearChkResult = " The graph is not linear" end if m ~= 0 then linearChkResult = " The graph is linear" end return " The slope is ".. m.. " and the y-intercept is ".. b.. ".".. graphDirResult .. ".".. linearChkResult .. "." end

How about something like this function? It’s not vastly shorter (in lines) but it’s a bit cleaner.

[lua]

local function slopeFinder( m, b )

    – Remember, in a linear graph, the function is y = mx + b find the intercept-- 

    print( “y = mx + b” )

    print( "y = "… m …"x + "…  b )

    print( "Slope is “… m …”, y-intercept is "…b )

    local graphDirResult = “(unknown)”

    local linearChkResult = “(unknown)”

    local function graphDir()

        if m > 0 then 

            graphDirResult = " Since the slope is "… m… “, the graph is positve. The direction will go up”

        elseif m < 0 then 

            graphDirResult = " Since the slope is "… m… “, the graph is negative. The direction will go down”

        elseif m == 0 then 

            graphDirResult = " Since the slope is "… m… “, the graph is constant. The direction is straight or horizontal”

        end

    end

    function linearChk()

        if m ~= 0 then

            linearChkResult = " The graph is linear"

        else

            linearChkResult = " The graph is not linear"

        end

    end

    graphDir()

    linearChk()

    return " The slope is "… m… " and the y-intercept is "… b… “.”… graphDirResult … “.”… linearChkResult … “.”

end

[/lua]

Then, call the function like this:

[lua]

local slopeFinderResult = slopeFinder( 2, 4 )

print( slopeFinderResult )

[/lua]

Best regards,

Brent

Thanks Brent. I’ll reconsider that next time.

local function slopeFinder( m, b ) -- Remember, in a linear graph, the function is y = mx + b find the intercept-- print( "y = mx + b" ) print( "y = ".. m .."x + ".. b ) print( "Slope is ".. m ..", y-intercept is "..b ) local graphDirResult = "(unknown)" local linearChkResult = "(unknown)" if m \> 0 then graphDirResult = " Since the slope is ".. m.. ", the graph is positve. The direction will go up" elseif m \< 0 then graphDirResult = " Since the slope is ".. m.. ", the graph is negative. The direction will go down" elseif m == 0 then graphDirResult = " Since the slope is ".. m.. ", the graph is constant. The direction is straight or horizontal" linearChkResult = " The graph is not linear" end if m ~= 0 then linearChkResult = " The graph is linear" end return " The slope is ".. m.. " and the y-intercept is ".. b.. ".".. graphDirResult .. ".".. linearChkResult .. "." end