Hướng dẫn thử thách
1 / 1
Restore a Coherent Narrative from an Array of Story Fragments
In this lab, you will restore a coherent narrative from a corrupted array of story fragments.
You will practice using loops by implementing fundamental array algorithms from scratch.
You will work with arrays of story fragment objects. Each fragment object has the following properties:
| Property | Description | Example value |
|-|-|-|
| `id` | A positive integer indicating the fragment's position in the story | `3` |
| `text` | The actual story content | `"and I use Arch btw.\""` |
In this lab, you are provided with a prefilled array called `shuffledFragments`.
**Objective**: Fulfill the user stories below and get all the tests to pass to complete the lab.
**User Stories**
1. You should not change the pre-filled `shuffledFragments` array.
2. You should create a function named `compactFragments` that takes an array of fragments and returns a new array with all undefined elements removed. If the function removes any undefined elements, it should log a message to the console. The message should start with the prefix `[COMPACTED]`.
3. You should declare a variable named `compactedShuffledFragments` and assign it the result of calling `compactFragments` with the `shuffledFragments` array.
4. You should create a function named `sortFragments` that takes an array of fragments without `undefined` elements and returns a new array sorted by the `id` property in ascending order, keeping fragments that share the same `id` in their original order. You should not use JavaScript's built-in `sort` method.
5. You should declare a variable named `sortedFragments` and assign it the result of calling `sortFragments` with the `compactedShuffledFragments` array.
6. You should create a function named `dedupeFragments` that takes a sorted array of fragments and returns a new array with duplicates removed, keeping only the first occurrence. You should define duplicates as two or more fragments sharing the same `id`. For each `id` that is deduplicated, the function should log a message to the console. The message should start with the prefix `[DEDUPED]`.
7. You should declare a variable named `dedupedFragments` and assign it the result of calling `dedupeFragments` with the `sortedFragments` array.
8. You should create a function named `fillMissingFragments` that takes a sorted array of fragments and returns a new array with missing fragments filled with placeholder objects. You should define missing fragments as gaps in the sequence between the lowest and highest `id`. The placeholder objects should have the format `{ id: missingId, text: "[...]" }`. For each placeholder added, the function should log a message to the console. The message should start with the prefix `[FILLED]`.
9. You should declare a variable named `filledFragments` and assign it the result of calling `fillMissingFragments` with the `dedupedFragments` array.
10. You should create a function named `assembleStory` that takes a sorted array of fragments and returns a single string containing all fragment texts, separated by newlines.
11. You should use `assembleStory` with your `filledFragments` to display the complete story in the console.
12. Your functions `compactFragments`, `sortFragments`, `dedupeFragments`, `fillMissingFragments` and `assembleStory` should not mutate the array that they are called with.
**Example**
Here is an example of an array containing story fragments:
```js
const exampleArray = [
{ id: 3, text: "and I use Arch btw.\"" },
,
{ id: 1, text: "Naomi said:" },
{ id: 3, text: "and I use Arch btw.\"" },
];
```
After restoring the story from `exampleArray`, it would look like this:
```md
Naomi said:
[...]
and I use Arch btw."
```
Vượt qua bài kiểm tra hiện tại để mở khóa bài tiếp theo.
main.swift
UTF-8 • Tab Size: 2Kiểm tra bài:⌘↵