Skip to content

Commit

Permalink
Add hints and solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough committed Feb 14, 2024
1 parent 14735d1 commit 144bf02
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
110 changes: 110 additions & 0 deletions src/content/codelabs/iterables.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,26 @@ String singleWhere(Iterable<String> items) {
}
```

<details>
<summary>Hint</summary>

Your solution might make use of the `contains` and `startsWith`
methods from the `String` class.

</details>

<details>
<summary>Solution</summary>

```dart
String singleWhere(Iterable<String> items) {
return items.singleWhere(
(element) => element.startsWith('M') && element.contains('a'));
}
```

</details>

## Checking conditions

When working with `Iterable`, sometimes you need to verify that
Expand Down Expand Up @@ -422,6 +442,30 @@ class User {
}
```

<details>
<summary>Hint</summary>

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).

</details>

<details>
<summary>Solution</summary>

```dart
bool anyUserUnder18(Iterable<User> users) {
return users.any((user) => user.age < 18);
}
bool everyUserOver13(Iterable<User> users) {
return users.every((user) => user.age > 13);
}
```

</details>

:::secondary Quick review
* Although you can use `for-in` loops to check conditions,
there are better ways to do that.
Expand Down Expand Up @@ -577,6 +621,30 @@ class User {
}
```

<details>
<summary>Hint</summary>

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).

</details>

<details>
<summary>Solution</summary>

```dart
Iterable<User> filterOutUnder21(Iterable<User> users) {
return users.where((user) => user.age >= 21);
}
Iterable<User> findShortNamed(Iterable<User> users) {
return users.where((user) => user.name.length <= 3);
}
```

</details>

:::secondary Quick review
* Filter the elements of an `Iterable` with `where()`.
* The output of `where()` is another `Iterable`.
Expand Down Expand Up @@ -656,6 +724,29 @@ class User {
}
```

<details>
<summary>Hint</summary>

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).

</details>

<details>
<summary>Solution</summary>

```dart
Iterable<String> getNameAndAges(Iterable<User> users) {
return users.map((user) => '${user.name} is ${user.age}');
}
```

</details>

:::secondary Quick review
* `map()` applies a function to all the elements of an `Iterable`.
* The output of `map()` is another `Iterable`.
Expand Down Expand Up @@ -740,6 +831,25 @@ class EmailAddress {
}
```

<details>
<summary>Solution</summary>

```dart
Iterable<EmailAddress> parseEmailAddresses(Iterable<String> strings) {
return strings.map((s) => EmailAddress(s));
}
bool anyInvalidEmailAddress(Iterable<EmailAddress> emails) {
return emails.any((email) => !isValidEmailAddress(email));
}
Iterable<EmailAddress> validEmailAddresses(Iterable<EmailAddress> emails) {
return emails.where((email) => isValidEmailAddress(email));
}
```

</details>

## What's next

Congratulations, you finished the codelab!
Expand Down
4 changes: 3 additions & 1 deletion src/content/language/built-in-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
```

<a id="string-interpolation"></a>

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.

<?code-excerpt "misc/test/language_tour/built_in_types_test.dart (string-interpolation)"?>
Expand Down

0 comments on commit 144bf02

Please sign in to comment.