Turning requirements into logic isn't always straightforward. 😅

Today, I built a Cargo Manifest Validator using JavaScript.

At first, the task seemed simple: validate a cargo manifest, normalize its weight, and process it.

But translating written requirements into working conditions turned out to be the real challenge.

Let's look at what I built and some JavaScript concepts I learned along the way.

🚢 Project Overview

A cargo manifest is a document containing information about goods being transported, such as their container ID, destination, weight, measurement unit, and whether they contain hazardous materials.

For this project, I represented a cargo manifest using a JavaScript object:

I implemented three functions to handle this data.

1. normalizeUnits()

The first requirement was to normalize cargo weight into kilograms.

If the weight was provided in pounds (lb), it needed to be converted into kilograms (kg).

But there was another requirement: Don't mutate the original object.

Here's how I approached it:

What I learned: Object immutability

Instead of modifying the original object, I used the spread operator:

//function must not mutate the original manifest object
let newManifest = {...manifest};

This creates a shallow copy of the object.

I could then update the copied object's properties without changing the original object's top-level properties.

This was a useful reminder that sometimes the solution isn't just about changing data, but also about preserving the original data.

2. validateManifest()

This was the part that required the most logical thinking.

I had to validate five properties:

containerId: Must be a positive integer.

destination: Must be a non-empty string.

weight: Must be a positive number.

unit: Must be either "kg" or "lb".

hazmat: Must be a boolean.

But here's where I encountered an important distinction:

A property can be missing, or it can exist with an invalid value.

These are two different situations.

For example:

let manifest = {
weight: -84
};

Here, containerId is missing, whereas weight exists but contains an invalid value.

I handled these situations separately:

JavaScript concepts I practiced here:

The in operator: Checks whether a property exists in an object.

Number.isInteger(): Checks whether a number is an integer.

typeof: Checks the type of a value.

.trim(): Removes whitespace from both ends of a string.

Number.isNaN(): Checks whether a value is NaN.

One particularly useful lesson was understanding that checking whether a property exists is not the same as checking whether its value is valid.

3. processManifest()

Finally, I needed to connect both functions into one workflow.

The requirement was to validate the manifest first and normalize its weight only if validation passed.

Here, I practiced another useful concept:

Object.keys(errors).length === 0

Object.keys() returns an array of an object's own enumerable property names.

If the errors object has no properties, its length is zero, meaning no validation errors were recorded.

This allowed me to control the processing flow.

🧪 Testing Different Scenarios

Writing the functions wasn't enough. I also needed to see how they behaved with different inputs.

Some scenarios I tested:

Test case

Expected result

Valid manifest

Successful validation

Missing container ID

Missing error

Negative container ID

Invalid error

NaN as weight

Invalid error

Unsupported unit

Invalid error

Empty object

Missing property errors

Testing different inputs helped me understand how individual conditions worked together.

💡 What Was Challenging?

Some of the biggest challenges I faced:

Understanding when to use the in operator.

Distinguishing missing properties from invalid values.

Translating requirements like "positive integer" into JavaScript conditions.

Understanding why object immutability mattered.

Connecting multiple functions into one working process.

There were moments when I knew what a JavaScript operator did, but couldn't figure out where or why I should use it.

That difference between knowing syntax and applying logic is something I'm actively working on.

🎯 My Biggest Takeaway

A programming problem isn't solved simply by knowing which methods and operators exist.

You need to understand the requirements, break them into smaller conditions, think about different possible inputs, and connect everything logically.

Every bug taught me something new about problem-solving and writing better code.

Building, breaking, learning, and repeating. That's the journey. 🚀

💬 Let's Discuss

When you're given a programming problem with multiple requirements, how do you approach it?

Do you break the requirements into smaller conditions before coding, or start experimenting with code and figure things out along the way?

I'd love to hear how other developers approach problem-solving!

JavaScript #WebDevelopment #BuildInPublic #Beginners #Programming