From 144bf02dfd879b154df7dd4963b6e72bacdd243f Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Tue, 13 Feb 2024 20:15:30 -0600 Subject: [PATCH] Add hints and solutions --- src/content/codelabs/iterables.md | 110 +++++++++++++++++++++++++ src/content/language/built-in-types.md | 4 +- 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/src/content/codelabs/iterables.md b/src/content/codelabs/iterables.md index be4a9d0661..44ef40f175 100644 --- a/src/content/codelabs/iterables.md +++ b/src/content/codelabs/iterables.md @@ -315,6 +315,26 @@ String singleWhere(Iterable items) { } ``` +
+ Hint + + Your solution might make use of the `contains` and `startsWith` + methods from the `String` class. + +
+ +
+ Solution + + ```dart + String singleWhere(Iterable items) { + return items.singleWhere( + (element) => element.startsWith('M') && element.contains('a')); + } + ``` + +
+ ## Checking conditions When working with `Iterable`, sometimes you need to verify that @@ -422,6 +442,30 @@ class User { } ``` +
+ Hint + + Remember to use the `any` and `every` methods from the `Iterable` class. + For help and examples using these methods, refer to + the [earlier discussion of them](#example-using-any-and-every). + +
+ +
+ Solution + + ```dart + bool anyUserUnder18(Iterable users) { + return users.any((user) => user.age < 18); + } + + bool everyUserOver13(Iterable users) { + return users.every((user) => user.age > 13); + } + ``` + +
+ :::secondary Quick review * Although you can use `for-in` loops to check conditions, there are better ways to do that. @@ -577,6 +621,30 @@ class User { } ``` +
+ Hint + + Remember to take advantage of the `where` method from the `Iterable` class. + For help and examples using `where`, refer to + the [earlier discussion of it](#example-using-where). + +
+ +
+ Solution + + ```dart + Iterable filterOutUnder21(Iterable users) { + return users.where((user) => user.age >= 21); + } + + Iterable findShortNamed(Iterable users) { + return users.where((user) => user.name.length <= 3); + } + ``` + +
+ :::secondary Quick review * Filter the elements of an `Iterable` with `where()`. * The output of `where()` is another `Iterable`. @@ -656,6 +724,29 @@ class User { } ``` +
+ Hint + + Remember to take advantage of the `map` method from the `Iterable` class. + For help and examples using `map`, refer to + the [earlier discussion of it](#example-using-map-to-change-elements). + + To concatenate multiple values into a single string, consider + using [string interpolation](/language/built-in-types#string-interpolation). + +
+ +
+ Solution + + ```dart + Iterable getNameAndAges(Iterable users) { + return users.map((user) => '${user.name} is ${user.age}'); + } + ``` + +
+ :::secondary Quick review * `map()` applies a function to all the elements of an `Iterable`. * The output of `map()` is another `Iterable`. @@ -740,6 +831,25 @@ class EmailAddress { } ``` +
+ Solution + + ```dart + Iterable parseEmailAddresses(Iterable strings) { + return strings.map((s) => EmailAddress(s)); + } + + bool anyInvalidEmailAddress(Iterable emails) { + return emails.any((email) => !isValidEmailAddress(email)); + } + + Iterable validEmailAddresses(Iterable emails) { + return emails.where((email) => isValidEmailAddress(email)); + } + ``` + +
+ ## What's next Congratulations, you finished the codelab! diff --git a/src/content/language/built-in-types.md b/src/content/language/built-in-types.md index afd2faa657..0849c9006d 100644 --- a/src/content/language/built-in-types.md +++ b/src/content/language/built-in-types.md @@ -187,9 +187,11 @@ var s3 = 'It\'s easy to escape the string delimiter.'; var s4 = "It's even easier to use the other delimiter."; ``` + + You can put the value of an expression inside a string by using `${`*`expression`*`}`. If the expression is an identifier, you can skip -the {}. To get the string corresponding to an object, Dart calls the +the `{}`. To get the string corresponding to an object, Dart calls the object's `toString()` method.