# print(fizz..buzz)

I couldn't help but notice we didn't have a FizzBuzz in yet. I've always wanted to take a shot at Lua, so here goes nothing.

### Code:

function fizzBuzz(maxNum)
local fizz = "Lua"
local buzz = "Yay"
for i = 1, maxNum do
if i % 15 == 0 then
print(fizz..buzz)
elseif i % 3 == 0 then
print(fizz)
elseif i % 5 == 0 then
print (buzz)
else
print(i)
end
end
end

fizzBuzz(100)


### Output:

1
2
Lua
4
Yay
Lua
7
8
Lua
Yay
11
Lua
13
14
LuaYay
16
17


I highly doubt it's idiomatic Lua, so I'm open for suggestions. As far as I know mixedCase and 2-space indentation are the way to go for Lua, but feel free to prove me wrong.

Lua, by definition is an embeddable scripting language. So, there are no guidelines as to how you want to create a variable or indent your code. It largely is a factor of how a developer feels comfortable.

Please note that although there is a style guide available for Lua, it actually is an amalgamation of Python, Perl, C and C++ style guides.

Two things, which I have seen common among a lot of lua programs/modules is that the function names (if not defined as a method of an object) are defined in snake_case, and if defined as a method to an object, use CamelCase (with both C capitalised). And another is, the variables for general data-types (number, string, function, table, boolean) follow a Hungarian notation.

In your , when calling the print function, you have given an extra space for the buzz output, whereas nothing as such for others. Keeping it consistent:

if i % 15 == 0 then
print(fizz..buzz)
elseif i % 3 == 0 then
print(fizz)
elseif i % 5 == 0 then
print(buzz)    -- remove whitespace
else
print(i)
end


You could also store the fizz..buzz value outside of the loop, just to avoid one extra calculation. Apart from that everything looks good.

• I hadn't noticed the extra whitespace, I agree it shouldn't have been there. Thank you for the heads-up about the Lua-way and the rest of the review. – Mast Aug 19 '16 at 9:23

While hjpotter92 is correct about it being up to the programmers, I can offer improvements upon your code:

local fizz = "Lua"
local buzz = "Yay"
--Keeping them out of scope means the function isn't redeclaring them each time it's run
function fizzBuzz(maxNum)
for i = 1, maxNum do
print(i%15==0 and fizz..buzz or i%5==0 and buzz or i%3==0 and fizz or i)
--By using Lua's logical operators you can cut down on massive chunks of code like the if statements you had made
end
end

fizzBuzz(100)

• You could also divide the code snippets with each comment so that they're easier to read (just include the local statements and the call to print()). – Jamal Sep 27 '16 at 2:05
• @jamal I would rather provide a function piece of code for them to full contrast against their own. – warspyking Sep 27 '16 at 3:30

Lua logical operators work a bit uniquely, so one can do:

function fizzbuzz(num)
return
num%15 == 0
and 'fizzbuzz'
or
num%5 == 0
and 'fizz'
or
num%3 == 0
and 'buzz'
or
tostring(num)
end

for i = 1,100 do
print(fizzbuzz(i))
end


Notice, this is literally the same as the previous answer, only formatted differently.

I love to use this style to describe logic, it is extremely easy to read.

• Good to know :-) – Mast Sep 18 '17 at 14:21
• Now this is top tier! – mid Aug 14 at 9:23