pigpigyyy / Yuescript

A Moonscript dialect compiles to Lua.
http://yuescript.org
MIT License
443 stars 38 forks source link

No output when running code in online compiler #148

Closed jaggedblades closed 1 year ago

jaggedblades commented 1 year ago

When running the following in the online compiler:

print_summation = (nums) ->
    sum = 0 
    for i, num in ipairs nums 
        sum += num
        io.write num
        if i == #nums
            io.write " = ", sum
            return
        io.write " + "

print_summation {1, 2, 3, 4, 5}

Nothing gets printed, when it should print:

1 + 2 + 3 + 4 + 5 = 15

Same issue when running it in the original moonscript online compiler http://moonscript.org/compiler/

pigpigyyy commented 1 year ago

The io.write function is not redirected to any web display, but you can try print instead. The following code should give you the expected result.

res = {}
print_summation = (nums) ->
    sum = 0 
    for i, num in ipairs nums 
        sum += num
        res[] = num
        if i == #nums
            res[] = " = "
            res[] = sum
            return
        res[] = " + "

print_summation {1, 2, 3, 4, 5}

print table.concat res
jaggedblades commented 1 year ago

Can you make io.write redirect to the web display?

pigpigyyy commented 1 year ago

Yes, you can clear the cache and try it again now.

jaggedblades commented 1 year ago

Alright, thanks 👍