Skip to content

Instantly share code, notes, and snippets.

@kenresoft
Last active August 10, 2023 02:10
Show Gist options
  • Save kenresoft/48af1501b30acbf49e19944e6384fc18 to your computer and use it in GitHub Desktop.
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.
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));
}
}
@kenresoft
Copy link
Author

The main() function defines a list of options and then calls the getColorForOption() function for each option. The getColorForOption() 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. The correctAnswer variable is used to determine whether the option is correct. If the option is selected, then the getColorForOption() function returns the color Green if the correctAnswer variable is true or the color Red if the correctAnswer variable is false. If the option is not selected, then the getColorForOption() function returns the color Green-Correction if the correctAnswer variable is true or the color Grey if the correctAnswer variable is false.

The for loop iterates over the list of options and calls the getColorForOption() function for each option. The print() function prints the color of the option.

@kenresoft
Copy link
Author

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