Skip to content

Instantly share code, notes, and snippets.

@miquelbeltran
Last active December 18, 2019 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miquelbeltran/c2ca9085b2376471b30569d570590894 to your computer and use it in GitHub Desktop.
Save miquelbeltran/c2ca9085b2376471b30569d570590894 to your computer and use it in GitHub Desktop.
Use map to create a String with the user.name and the user.age
Iterable<String> getNameAndAges(Iterable<User> users) {
// implement this method
}
class User {
String name;
int age;
User(
this.name,
this.age,
);
}
Iterable<int> getNameAndAges(Iterable<User> users) {
return users.map((user) => '${user.name} is ${user.age}');
}
var users = [
User('Alice', 21),
User('Bob', 17),
User('Claire', 52),
];
void main() {
try {
final out = getNameAndAges(users).toList();
if (out == null) {
_result(false, [
'Tried running `getNameAndAges`, but received a null value. Did you implement the method?'
]);
return;
}
if (!_listEquals(out, ['Alice is 21', 'Bob is 17', 'Claire is 52'])) {
_result(false, ['Looks like `getNameAndAges` is wrong. Keep trying! The output was $out']);
return;
}
_result(true);
} catch (e) {
_result(false, ['Tried running the method, but received an exception: $e']);
}
}
bool _listEquals<T>(List<T> a, List<T> b) {
if (a == null)
return b == null;
if (b == null || a.length != b.length)
return false;
for (int index = 0; index < a.length; index += 1) {
if (a[index] != b[index])
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment