FizzBuzz Solution Dumping Ground


#244

in JS, king of the web
for(var i = 0; i<100; i++){
if(i % 15 === 0)
console.log(“fizzBuzz”);
else if(i % 3 === 0)
console.log(“fizz”);
else if(i % 5 === 0)
console.log(“buzz”);
else
console.log(i);
}


#245

In Crystal, which is kind of like a compiled ruby-esque language:

Range.new(1, 100).each do |n|
  msg = ""
  if n.modulo(3) == 0
    msg = msg + "Fizz"
  end
  if n.modulo(5) == 0
    msg = msg + "Buzz"
  end
  if msg == ""
    puts n
  else 
    puts msg
  end
end

Not perfect but I just started learning the language, and the language itself isn’t production-ready.


#246

Haven’t seen a GAS solution yet. Well, it’s mostly just JS, but I can cheat a bit.

My first attempt just used Logger.log() and was kind of direct brute force, but worked. Though technically, I failed since, in retrospect, the Logger method doesn’t really satisfy ‘print’ from an end user perspective and the Logger ended up string converting i with 1 decimal place. Took about 2 min.

function fizzBuzz() {
  for (var i=1;i<=100;i++){
    if (i%3 == 0 && i%5 == 0){
      Logger.log('FizzBuzz');
    } else if (i%3 == 0) {
      Logger.log('Fizz');
    } else if (i%5 == 0) {
      Logger.log('Buzz');
    } else {
      Logger.log(i);
    }
  }
}

Realizing I technically failed, took an extra 5 to optimise a touch while still having it legible and output as a web app. With GAS, you can kind of cheat and provide partial HTML. It will fill in any headers and such that you’re missing, so the client side works.

Here’s the final:

function doGet() {
  var str, res = '';
  for (var i=1;i<=100;i++){
    if (i % 3 == 0) str += 'Fizz';
    if (i % 5 == 0) str += 'Buzz';
    if (!str) str = i;
    res += str + '<br>';
    str = '';
  }
  return HtmlService.createHtmlOutput(res);
}

I’m not a programmer by trade, but I do dabble in a lot of GAS (which leads to client side JS, jQuery, html, css, etc.), and (thankfully much less frequently) VBA stuff related to my job. Fairly happy with 7 minutes for a working web app that meets the goal while not technically being a programmer. And no, not cheating. Replace exec in the url with edit to see the script.

Frankly shocking to read a lot of these from people who are programmers and missed some of the basic requirements.


#247

Just the tiniest bit disappointed to find only one PowerShell solution posted. Does no-one respect this language?
In any case, my own PS piece is a bit different so I’ll share for SnG:

$List = @(1..100)
$Fizz = 3
$Buzz = 5
ForEach ($Num In $List) {
	$Output = $Null
	If ($Num % $Fizz -EQ 0) {
		$Output += "Fizz"
	}
	If ($Num % $Buzz -EQ 0) {
		$Output += "Buzz"
	}
	If ($Output) {
		$Output
	} Else {
		$Num
	}
}

#248

I can’t believe there isn’t any Prolog in here.


fizzBuzzPrint(A):- A mod 15 =:= 0,display(‘FizzBuzz\n’).
fizzBuzzPrint(A):- A mod 5 =:= 0,display(‘Buzz\n’).
fizzBuzzPrint(A):- A mod 3 =:= 0,display(‘Fizz\n’).
fizzBuzzPrint(A):- display(A), display(’\n’).

fizzBuzz(B,B):- fizzBuzzPrint(B).
fizzBuzz(A,B):- fizzBuzzPrint(A), C is A+1, fizzBuzz(C,B),!.

?- fizzBuzz(1,100).


#249

PHP?
Didn’t program for ages, but heck, don’t understand why this should be difficult.
I prefer readability over short code…
I know 1) recursion takes a lot of heap, but I simply like it.
I know 2) yep, type-checking is not PHP strongest point, but heck, sometimes useful, see below…

<?php
function FizzBuzz ($n) {
 if ($n>0) {
	FizzBuzz($n-1) ;
	$fb="" ;
	if ($n%3==0) { $fb = "Fizz" ; }
	if ($n%5==0) { $fb .= "Buzz" ; }
	$fb = ($fb=="") ? $n : $fb ;
	echo $fb."\n" ;
 }
}

FizzBuzz (100) ;
?>

#251

and not a comment in any of them… you’re all fired.


#252

Can I have a retry pls dear manager…

<?php
function FizzBuzz ($n) {

// Define the array with dividers and what to shout
 $match = array (3 => "Fizz", 5 => "Buzz", 7 => "Woof", 11 => "Waf" ) ;
 if ($n>0) {
        // Recursively call FizzBuzz when $n > 0
	FizzBuzz($n-1) ;
        // $fb defines what to shout
	$fb="" ;
        // Cycle through the $match array and if we found a match add it to the output.
	foreach ( $match as $div => $shout) if ($n%$div == 0) $fb .= $shout ;
	$fb = ($fb=="") ? $n : $fb ; // If there is nothing to shout show the number
	echo $fb."\n" ;
        // FizzBuzz($n-1) ; // Put FizzBuzz here if you want it in reverse order
 }
}

FizzBuzz (100) ;
?>

#253

It’s wrong. No “Echo i” here


#254

It’s been a while since I’ve used python but you seem to have confused return and print. I don’t think this will output anything outside of the interpreter.


#255

Unless I’m missing something this is really easy, I didn’t think about it very hard.

using System;
namespace fizzbuzz {
    class Program {
        static void Main(string[] args) {
            for (int i = 1; i < 101; i++) {
                if (i % 3 == 0) Console.Write("Fizz");
                if (i % 5 == 0) Console.Write("Buzz");
                if (i % 3 != 0 && i % 5 != 0) Console.Write(i);
                Console.WriteLine("");
            }
            Console.Read();
        }
    }
}

#256

Just ran across this article… So, I decided to share the scary one. My PAINFUL submission is:

Applesoft Basic

Yes, you heard that right! I am writing this in APPLESOFT BASIC.

10 I = 0
15 E = 100
20 I = I + 1
30 IF ((I/15) >< INT(I/15)) THEN 40
35 PRINT "FizzBuzz"
38 IF I < E THEN 20
39 IF I = E THEN 80
40 IF ((I/5) >< INT(I/5)) THEN 50
45 PRINT "Buzz"
48 IF I < E THEN 20
49 IF I = E THEN 80
50 IF ((I/3) >< INT(I/3)) THEN 60
55 PRINT "Fizz"
58 IF I < E THEN 20
59 IF I = E THEN 80
60 Print I
70 IF I < E THEN 20
80 END

You can check my code at:
http://www.calormen.com/jsbasic/

And then you can program in APPLESOFT BASIC also.

Now, are you in pain yet??? I know I am.

Drat… I am breaking all the rules here…

Does your reply improve the conversation in some way? - It is APPLESOFT BASIC! So, no…

Be kind to your fellow community members. - I am posting APPLESOFT BASIC… so no…

Constructive criticism is welcome, but criticize ideas, not people. - I am sure APPLESOFT BASIC still breaches this one also…


#257

I like matlab. This is 81 character.

k=num2cell(1:100);k(3:3:99)={'Fizz'};k(5:5:100)={'Buzz'};k(15:15:90)={'FizzBuzz'}

#258

To show the fizzBuzz you should write the if (((i % 5) == 0) && ((i % 3) == 0)) at the top of the conditions list.
otherwise, it will not display the fizzbuzz, that’s logic :slight_smile:

C# code:

class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 100; i++) {
                
                   if (((i % 5) == 0) && ((i % 3) == 0))
                    {
                        Console.WriteLine("fizz Wizz");
                    }
                   else  if ((i % 3) == 0)
                    {
                        Console.WriteLine("fizz");
                    }
                    else if ((i % 5) == 0)
                    {
                        Console.WriteLine("Wizz");
                    }
                    else
                    Console.WriteLine(i);
                
            }
            Console.ReadLine();
        }
    }

#259
    for(int i=0; i<=100; i++){
            if ( i%3==0 || i%5==0){
                    if( i % 3 == 0 ) printf("Fizz");
                    if( i % 5 == 0 ) printf("Buzz");
            }
            else printf("%d",i);
            printf("\n");
    }

#260

C#

Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 100).Select(n => n % (3 * 5) == 0 ? "FizzBuzz" : n % 3 == 0 ? "Fizz" : n % 5 == 0 ? "Buzz" : $"{n}").ToArray()));

Or

Enumerable.Range(1, 100).Select(n => n % (3 * 5) == 0 ? "FizzBuzz" : n % 3 == 0 ? "Fizz" : n % 5 == 0 ? "Buzz" : $"{n}").ToList().ForEach(n => Console.WriteLine(n));

#261
for i in range(1, 101):
	if (i % 3) == 0 and (i % 5) == 0:
		print('FizzBuzz')
	elif (i % 3) == 0:
		print('Fizz')
	elif (i % 5) == 0:
		print('Buzz')
	else:
		print(i)

#262
 = (1<<14);*/
	for(int i=1;i<101;i++) {
		if(i%3 ==0 && i%5 != 0) {
			printf("fizz \n");
		}else if(i%3 != 0&& i%5==0) {
            printf("buzz \n");
		}else if( i%3 ==0 && i%5==0) {
			printf("fizzbuzz \n");

		}else {
           printf("%d \n",i);
		}

#263

I didn’t see an elixir solution here so:

Enum.map(1..100, fn x ->
  case {rem(x, 15) == 0, rem(x, 5) == 0, rem(x , 3) == 0} do
    {true, _, _} -> "FizzBuzz"
    {_, true, _} -> "Buzz"
    {_, _, true} -> "Fizz"
    _ -> x
  end
end)

#265

I saw no Go, so:

Get a list:

func fizzbuzzList(n int) []string {
	var res []string
	for i := 1; i <= n; i++ {
		switch {
		case i%15 == 0:
			res = append(res, `fizzbuzz`)
		case i%5 == 0:
			res = append(res, `buzz`)
		case i%3 == 0:
			res = append(res, `fizz`)
		default:
			res = append(res, strconv.Itoa(i))
		}
	}
	return res
}

And in a lazy manner (Future? Continuation?):

func fizzbuzzLazy(n int) chan string {
	var res = make(chan string)
	go func() {
		for i := 1; i <= n; i++ {
			switch {
			case i%15 == 0:
				res <- `fizzbuzz`
			case i%5 == 0:
				res <- `buzz`
			case i%3 == 0:
				res <- `fizz`
			default:
				res <- strconv.Itoa(i)
			}
		}
		close(res)
	}()
	return res
}