• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Question Fizzbuzz Coding Challage

Cecil

17+ years progress programming and still learning.
#1
I saw a YouTube video the other day and I thought it was quite interesting about hiring programmers and asking them the write a simple piece of code.

So today I thought I actually write the FizzBuzz game in the ABL and I came up with 3 alternatives which did the same thing.

What alternative code could you come up with in creating the FizzBuzz game?

1st
Code:
define variable i         as integer   no-undo.
define variable result as character no-undo.

do i = 1 to 100:
       
    if i MOD 3 ne 0 and i MOD 5 ne 0 then
    do:
        result = result + string(i) + '~r~n'.
        next.
    end.  
       
    if i MOD 3 eq 0 then          
        result = result + 'Fizz'.

    if i MOD 5 eq 0 then          
        result = result + 'Buzz'.
       
    result = result + '~r~n'.    
   
end.

message result
    view-as alert-box info.
2nd:
Code:
define variable i         as integer   no-undo.
define variable result as character no-undo.

do i = 1 to 100:
       
    case true:      
        when (i MOD 3 eq 0 and i MOD 5 eq 0) then
            result = result + 'FizzBuzz~r~n'.
        when (i MOD 3 eq 0) then          
            result = result + 'Fizz~r~n'.
        when (i MOD 5 eq 0) then          
            result = result + 'Buzz~r~n'.
        otherwise          
            result = result + string(i) + '~r~n'.
    end case.      
   
end.

message result
    view-as alert-box info.
3rd (I think this is my most elegant one I wrote):

Code:
define variable i         as integer   no-undo.
define variable result as character no-undo.

do i = 1 to 100:

    assign
        result = result + string(i) when (i MOD 3 ne 0 and i MOD 5 ne 0)
        result = result + 'Fizz'    when (i MOD 3 eq 0)
        result = result + 'Buzz'    when (i MOD 5 eq 0)
        result = result + '~r~n'.  

end.

message result
    view-as alert-box info.
 
Last edited:

Cecil

17+ years progress programming and still learning.
#2
Same as #2, but a little bit more normalized:

Code:
define variable i         as integer   no-undo.
define variable result as character no-undo.

&SCOPED-DEFINE fizz 3
&SCOPED-DEFINE buzz 5
&SCOPED-DEFINE fizzTxt Fizz
&SCOPED-DEFINE buzzTxt Buzz
&SCOPED-DEFINE newline chr(13) + chr(10)


do i = 1 to 100:
       
    case true:      
        when (i MOD {&fizz} eq 0 and i MOD {&buzz} eq 0) then
            result = result + '{&fizzTxt}{&buzzTxt}' + {&newline}.
        when (i MOD {&fizz} eq 0) then          
            result = result + '{&fizzTxt}' + {&newline}.
        when (i MOD {&buzz} eq 0) then          
            result = result + '{&buzzTxt}'  + {&newline}.
        otherwise          
            result = result + string(i) + {&newline}.
    end case.      
   
end.

message result
    view-as alert-box info.
 
Last edited:

Cringer

ProgressTalk.com Moderator
Staff member
#3
For a bit of fun, with recursion:
Code:
define variable result as character   no-undo.

run FizzBuzz(1).

message result
    view-as alert-box info buttons ok.

procedure FizzBuzz:
    define input  parameter MyInt as integer     no-undo.

    if MyInt gt 100 then
        return. 

    case true:      
        when (MyInt MOD 3 eq 0 and MyInt MOD 5 eq 0) then
            result = result + 'FizzBuzz~r~n'.
        when (MyInt MOD 3 eq 0) then          
            result = result + 'Fizz~r~n'.
        when (MyInt MOD 5 eq 0) then          
            result = result + 'Buzz~r~n'.
        otherwise          
            result = result + string(MyInt) + '~r~n'.
    end case. 

    run FizzBuzz(MyInt + 1).
end.
 

Cringer

ProgressTalk.com Moderator
Staff member
#4
A variation on your #3 because I like the fact you can do this in Progress.
Code:
define variable i         as integer   no-undo.
define variable result as character no-undo.

do i = 1 to 100:

    assign
        result = result + string(i) when (i MOD 3 ne 0 and i MOD 5 ne 0)
        result = result + trim(string((i MOD 3 eq 0),'Fizz/'))
        result = result + trim(string((i MOD 5 eq 0),'Buzz/'))
        result = result + '~r~n'.  

end.

message result
    view-as alert-box info.