Last active
March 17, 2022 10:07
-
-
Save hotdang-ca/0401f20f335463d35817e38762410786 to your computer and use it in GitHub Desktop.
Fizzbuzzing Dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Oct 23, 2020 | |
/// Updated March 17, 2021 | |
/// (c) 2020 James Robert Perih c/o Four And A Half Giraffes, Ltd. | |
void main() { | |
const int theStart = 1; | |
const int theEnd = 100; | |
DartFlutterOutputFactory().printIteration( | |
from: theStart, | |
to: theEnd, | |
isDartyMode: false, | |
); | |
} | |
class DartFlutterOutputFactory { | |
/// A method to obfiscate iterating over <s>fizzbuzz</s> FlutterDart. | |
/// | |
/// Starts counting from [from], up to and including [to]. If [isDartyMode] set, | |
/// will utilize syntax only Dart could use. If unset, will use a plain old port | |
/// of c code | |
void printIteration({ | |
required int from, | |
required int to, | |
bool isDartyMode = true, | |
}) { | |
for (int itr = from; itr <= to; itr++) { | |
print(isDartyMode | |
? DartFlutterOutputFactory.traditionalOutputForNumber(itr) | |
: DartFlutterOutputFactory.outputForNumber(itr)); | |
} | |
} | |
/// Concatenate strings based on divisbility of 3 and/or 5 | |
static String traditionalOutputForNumber(int number) { | |
String textOutput = ''; | |
if (number % 5 == 0) { | |
textOutput = '${textOutput}Dart'; | |
} | |
if (number % 15 == 0) { | |
textOutput = '$textOutput loves '; | |
} | |
if (number % 3 == 0) { | |
textOutput = '${textOutput}Flutter'; | |
} | |
return textOutput.isNotEmpty ? textOutput : '$number'; | |
} | |
/// Plain old return statement. Cleaner. Algorithm nerds don't like it, but it's the same time + complexity. | |
static String outputForNumber(int number) { | |
if (number % 3 == 0 && number % 5 == 0) return 'Dart loves Flutter'; | |
if (number % 3 == 0) return 'Dart'; | |
if (number % 5 == 0) return 'Flutter'; | |
return '$number'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment