Hướng dẫn thử thách
1 / 1
Build a Cargo Manifest Validator
In this lab, you will use JavaScript to normalize and validate cargo manifests. A cargo manifest is a document that typically lists goods being transported (for example, by ship or train) and includes details about those goods.
Each cargo manifest will be represented as an object with the following properties:
- `containerId`: a positive integer identifying the associated cargo container.
- `destination`: a non-empty string (after trimming whitespace) denoting the cargo's target destination.
- `weight`: a positive number representing the cargo's weight.
- `unit`: a string describing the unit of the cargo's `weight` property (either `"kg"` for kilograms or `"lb"` for pounds).
- `hazmat`: a boolean value indicating whether hazardous material handling is needed.
Example cargo manifest object:
```js
{
containerId: 1,
destination: "Monterey, California, USA",
weight: 831,
unit: "lb",
hazmat: false
}
```
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
**User Stories:**
1. You should implement a function named `normalizeUnits` with a `manifest` parameter.
- The function must not mutate the original manifest object and must always return a new object where `weight` is normalized to kilograms and `unit` is set to `"kg"`.
- If the `weight` of the manifest object is expressed in pounds (`unit: "lb"`), the function should convert the `weight` to kilograms using the approximate conversion `1 lb = 0.45 kg`, and update the `unit` accordingly.
- If the `weight` is already expressed in kilograms (`unit: "kg"`), the `weight` and `unit` should remain unchanged.
2. You should implement a function named `validateManifest` with a `manifest` parameter.
- The function must not mutate the original manifest object and must always return a new object.
- If the input manifest is valid (no missing or invalid properties), the function should return an empty object.
- If the input manifest is not valid, the function should return an object containing entries for each missing or invalid property. Missing properties should have the value `"Missing"` and invalid properties should have the value `"Invalid"`.
Example return value where the input object is missing the `destination` property and has an invalid `weight` property:
```js
{
destination: "Missing",
weight: "Invalid"
}
```
3. You should implement a function named `processManifest` with a `manifest` parameter. The function should log:
- If the manifest object is valid, `Validation success: ${containerId}` and then the manifest's `weight` in kilograms, in the form `Total weight: ${weight} kg`. Use `normalizeUnits()` for this conversion.
- If the manifest object is not valid, `Validation error: ${containerId}` and then the object returned by calling `validateManifest()` with the manifest object.
**Note:** Each of these two cases should have two `console.log()` calls.
**Note:** Do not declare `normalizeUnits`, `validateManifest`, or `processManifest` using `const`, since the tests need to reassign them.
# --before-each--
```js
const _validManifests = [
{
containerId: 4,
destination: "Monterey, California, USA",
weight: 831,
unit: "lb",
hazmat: false
},
{
containerId: 5,
destination: "Montreal, Quebec, Canada",
weight: 151,
unit: "kg",
hazmat: false
}
];
const _invalidManifests = [
{
containerId: -6,
destination: 123,
weight: 0,
unit: "pounds",
hazmat: true
},
{
containerId: 0,
weight: -21,
unit: "KG"
},
{
containerId: "eight",
destination: "Guadalajara, Jalisco, Mexico",
weight: 9001,
hazmat: "no"
},
{
destination: " ",
weight: NaN,
unit: "kg",
hazmat: 1
},
{
containerId: 10,
destination: " ",
},
];
const _testObj = {
containerId: 1.51,
destination: "Lavender Town",
weight: NaN,
};
console.log = () => {};
```
Vượt qua bài kiểm tra hiện tại để mở khóa bài tiếp theo.
main.rs
UTF-8 • Tab Size: 2Kiểm tra bài:⌘↵