{"id":1145,"date":"2012-10-12T01:03:06","date_gmt":"2012-10-12T07:03:06","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1145"},"modified":"2012-11-30T21:50:51","modified_gmt":"2012-12-01T03:50:51","slug":"code-of-the-day-fizzbuzz-in-javascript","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/10\/code-of-the-day-fizzbuzz-in-javascript\/","title":{"rendered":"Code of the day &#8211; FizzBuzz in Javascript"},"content":{"rendered":"<p>FizzBuzz Test<\/p>\n<blockquote><p>\nWrite a program that prints the numbers from 1 to 100. But for multiples of three print \u201cFizz\u201d instead of the number and for the multiples of five print \u201cBuzz\u201d. For numbers which are multiples of both three and five print \u201cFizzBuzz\u201d.<br \/>\n<br \/>&#8212;<cite><a href=\"http:\/\/c2.com\/cgi\/wiki?FizzBuzzTest\">c2.com<\/a><\/cite>\n<\/p><\/blockquote>\n<p>Here&#8217;s my solution:<br \/>\n<b>Test Driven Development Approach<\/b><br \/>\n<code><\/p>\n<pre lang='javascript'>\t\r\n\/\/ TDD Readable\r\nvar getFizzBuzzStatements = function (len) {\r\n\tvar arr = [],\r\n\tstr = \"\";\r\n\tfor (var i = 1; i <= len; i++) {\r\n\t\tstr = (i % 3) ? \"\" : \"Fizz\";\r\n\t\tstr += (i % 5) ? \"\" : \"Buzz\";\r\n\t\tarr.push(str||i);\r\n\t}\r\n\treturn arr;\r\n};\r\nvar fizzBuzz = function () {\r\n\tconsole.log(getFizzBuzzStatements(100).join(\"\\n\"));\r\n};\r\nfizzBuzz();\r\n<\/pre>\n<p><\/code><\/p>\n<p><b>Alternative: One liner<\/b><br \/>\n<code><\/p>\n<pre lang='javascript'>\r\n\/\/ One liner\r\nfor (i = 1; i <= 100; i++)\r\n\tconsole.log((((i % 3) ? \"\" : \"Fizz\") + (i % 5 ? \"\" : \"Buzz\")||i) + \"\\n\")\r\n<\/pre>\n<p><\/code><\/p>\n<p><b>Alternative: Switches<\/b><br \/>\n<code><\/p>\n<pre lang='javascript'>\r\nvar getFizzBuzzStatements = function (len) {\r\n\tvar j,\r\n\tarr = [];\r\n\tfor (var i = 1; i <= len; i++) {\r\n\t\tswitch (i % 15) {\r\n\t\tcase 0:\r\n\t\t\tarr.push(\"FizzBuzz\");\r\n\t\t\tbreak;\r\n\t\tcase 3:\tcase 6:\tcase 9:\tcase 12:\r\n\t\t\tarr.push(\"Fizz\");\r\n\t\t\tbreak;\r\n\t\tcase 5:\tcase 10:\r\n\t\t\tarr.push(\"Buzz\");\r\n\t\t\tbreak;\r\n\t\tdefault:\r\n\t\t\tarr.push(i);\r\n\t\t}\r\n\t}\r\n\treturn arr;\r\n};\r\nvar fizzBuzz = function () {\r\n\tconsole.log(getFizzBuzzStatements(100).join(\"\\n\"));\r\n};\r\n<\/pre>\n<p><\/code><\/p>\n<p><b>Demo<\/b><br \/>\n<iframe style=\"width: 100%; height: 300px\" src=\"http:\/\/jsfiddle.net\/q2akw\/embedded\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>FizzBuzz Test Write a program that prints the numbers from 1 to 100. But for multiples of three print \u201cFizz\u201d instead of the number and for the multiples of five print \u201cBuzz\u201d. For numbers which are multiples of both three and five print \u201cFizzBuzz\u201d. &#8212;c2.com Here&#8217;s my solution: Test Driven Development Approach \/\/ TDD Readable &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/10\/code-of-the-day-fizzbuzz-in-javascript\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the day &#8211; FizzBuzz in Javascript<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,62],"tags":[149,164,150],"class_list":["post-1145","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-questions-and-answers","tag-fizzbuzz","tag-javascript","tag-qa"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1145"}],"version-history":[{"count":7,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1145\/revisions"}],"predecessor-version":[{"id":1165,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1145\/revisions\/1165"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}