Skip to content

Instantly share code, notes, and snippets.

@ericwindmill
Created May 10, 2020 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericwindmill/9f5065991b2194a12fc03fbf7c0fc0e3 to your computer and use it in GitHub Desktop.
Save ericwindmill/9f5065991b2194a12fc03fbf7c0fc0e3 to your computer and use it in GitHub Desktop.
fbe_dart_iterables_filter_where_takewhile
This will require using `where` and `skipWhile` together
List<int> getOddNumsOver10(List<int> nums) {
// implement the logic to take any list of integers and return
// a new list that contains only the numbers in the list over 10 and that are odd.
//
// for example: List<int> sortedNums = [2, 5, 7, 10, 12, 19, 92, 106]
// would be transformed into [19]
}
List<int> getOddNumsOver10(List<int> nums) {
return nums.skipWhile((int n) => n < 11).where((int i) => i % 2 != 0).toList();
}
List<int> sortedNums = [2, 5, 7, 10, 12, 19, 92, 106];
void main() {
final oddNumsOver10 = getOddNumsOver10(sortedNums);
if (oddNumsOver10.contains(2) ||
oddNumsOver10.contains(5) ||
oddNumsOver10.contains(7) ||
oddNumsOver10.contains(10)) {
_result(false, ['the function getOddNumsOver10 should not contain any numbers less than 11']);
}
if (oddNumsOver10.any((int i) => i % 2 == 0)) {
_result(false, ['the function getOddsOver10 should not contain any even numbers']);
}
_result(true, ['All tests passed.', 'yeet']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment