Last active
August 10, 2023 02:10
-
-
Save kenresoft/48af1501b30acbf49e19944e6384fc18 to your computer and use it in GitHub Desktop.
A function that returns the color of an option based on whether the option is correct and whether the option is selected.
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(List<String> arguments) { | |
options = ['Multiply', 'Dart', 'Function', 'Gold', 'Silver', 'Bronze']; | |
String getColorForOption(String answer) { | |
bool correctAnswer = answer.startsWith("S"); | |
bool isSelected = answer.startsWith("D"); | |
if (isSelected) { | |
return correctAnswer ? 'Green' : 'Red'; | |
} else { | |
return correctAnswer ? 'Green-Correction' : 'Grey'; | |
} | |
} | |
for (var answer in options) { | |
print(getColorForOption(item)); | |
} | |
} |
Output
Grey
Red
Grey
Grey
Green-Correction
Grey
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
main()
function defines a list of options and then calls thegetColorForOption()
function for each option. ThegetColorForOption()
function takes a string as input and returns a string representing the color of the option. The color of the option depends on two factors: whether the option is correct and whether the option is selected.The
isSelected
variable is used to determine whether the option is selected. ThecorrectAnswer
variable is used to determine whether the option is correct. If the option is selected, then thegetColorForOption()
function returns the colorGreen
if thecorrectAnswer
variable is true or the colorRed
if thecorrectAnswer
variable is false. If the option is not selected, then thegetColorForOption()
function returns the colorGreen-Correction
if thecorrectAnswer
variable is true or the colorGrey
if thecorrectAnswer
variable is false.The
for
loop iterates over the list of options and calls thegetColorForOption()
function for each option. Theprint()
function prints the color of the option.