Monday, January 29, 2007

FizzBuss for Programmers

I read this post about a FizzBuzz test for programmers (which I was pointed to by Joel's Reddit page) and decided to give it a go. I like doing exercises like these for fun of them and because I’ve been “promoted” in my career to the point where I don’t get to write much code on the job. Doing exercises like these keeps the mind fresh and allows me to remember what it’s like to write code. Probably took about 5 minutes but that’s because I never try to remember syntax. I just look it up when I need it.

var output = "";

for( var i = 1; i < 101; i++ ){
if (i == 1){ output = output + i; }
else if ( i % 3 == 0 && i % 5 == 0){ output = output + ", FizzBuzz"; }
else if ( i % 3 == 0){ output = output + ", Fizz"; }
else if ( i % 5 == 0 ){ output = output + ", Buzz"; }
else { output = output + ", " + i; }
}

alert(output);

No comments: