![]() ![]() Tested the output, added some comments and BOOM! FizzBuzz. I ended up throwing in some conditional/ternary operators to replace the “IF” statements. Once I realized I could solve it in one line, I then tried to see what Java idioms I could throw in. ![]() I then proceeded to simplify it repeatedly until I realized I could solve it in one line. To give you an idea of what you’re looking at, first, I solved it in the most obvious way possible. If "i" is divisible by 3 and 5 return "FizzBuzz"Įlse if "i" is divisible by 3 return "Fizz"Įlse if "i" is divisible by 5 return "Buzz"Įlse return int value of "i" with an empty string appended to cast to String */ It's essentially one big assignment inside of a print statement. *Solution uses the conditional or ternary operator to make decisions instead of if statements. It just seems so simple right? If you’re unfamiliar with FizzBuzz, the prompt is at the top of each code snippet below:įirst, here’s FizzBuzz in multiple lines: /* Write a short program that prints each number from 1 to 100 on a new line.įor each multiple of 3, print "Fizz" instead of the number.įor each multiple of 5, print "Buzz" instead of the number.įor numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.Īnd here’s FizzBuzz in one line: /* Write a short program that prints each number from 1 to 100 on a new line. I’d heard of FizzBuzz before and had thought through solving it but I’d never actually tried to solve it. While looking up common programming interview questions, I realized that I’d never actually solved FizzBuzz.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |