Last active
December 18, 2019 08:52
-
-
Save miquelbeltran/c2ca9085b2376471b30569d570590894 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Use map to create a String with the user.name and the user.age |
This file contains hidden or 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
Iterable<String> getNameAndAges(Iterable<User> users) { | |
// implement this method | |
} | |
class User { | |
String name; | |
int age; | |
User( | |
this.name, | |
this.age, | |
); | |
} |
This file contains hidden or 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
Iterable<int> getNameAndAges(Iterable<User> users) { | |
return users.map((user) => '${user.name} is ${user.age}'); | |
} |
This file contains hidden or 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
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