Created
July 2, 2020 07:27
-
-
Save tomgilder/7121cb5588b7e0fcd9511568c0e0fe32 to your computer and use it in GitHub Desktop.
enum counting
This file contains 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
void main() { | |
final words = ['a', 'dog', 'apples', 'supercalifragilisticexpialidocious']; | |
final lengths = words.map((word) => getWordLength(word)); | |
print("Long words: ${lengths.where((length) => length == WordLength.long).length}"); | |
print("Medium words: ${lengths.where((length) => length == WordLength.medium).length}"); | |
print("Short words: ${lengths.where((length) => length == WordLength.short).length}"); | |
} | |
WordLength getWordLength(String word) { | |
if (word.length > 10) { | |
return WordLength.long; | |
} | |
if (word.length > 5) { | |
return WordLength.medium; | |
} | |
return WordLength.short; | |
} | |
enum WordLength { | |
short, | |
medium, | |
long | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment