Skip to content

Instantly share code, notes, and snippets.

@markylaredo
Last active May 1, 2023 11:23
Show Gist options
  • Save markylaredo/438cb1d06190ba4dc3bf1646a7300b5c to your computer and use it in GitHub Desktop.
Save markylaredo/438cb1d06190ba4dc3bf1646a7300b5c to your computer and use it in GitHub Desktop.
This is a simple JavaScript program that calculates the total freight volume, weight (in pounds), volumetric weight for sea shipment
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CBM Calculator</title>
</head>
<body>
<label for="length">Length (cm):</label>
<input id="length" placeholder="Length (cm)" type="number"><br>
<label for="width">Width (cm):</label>
<input id="width" placeholder="Width (cm)" type="number"><br>
<label for="height">Height (cm):</label>
<input id="height" placeholder="Height (cm)" type="number"><br>
<label for="weight">Parcel Parcel Gross Weight (kg):</label>
<input id="weight" placeholder="Parcel Parcel Gross Weight (kg)" type="number">
<br>
<label for="quantity">Parcel Quantity:</label>
<input id="quantity" placeholder="Parcel Quantity" type="number"><br>
<br>
<br>
<button id="calculate">Calculate</button>
<br>
<br>
<label for="totalFreight">Total freight volume:</label>
<label id="totalFreight"></label> <br>
<label for="weightLb">Weight (lb):</label>
<label id="weightLb"></label> <br>
<label for="seaVolumetricWeight">Volumetric weight for sea shipment:</label>
<label id="seaVolumetricWeight"></label> <br>
<label for="airVolumetricWeight">Volumetric weight for air shipment:</label>
<label id="airVolumetricWeight"></label>
<script>
// Get the input elements and the calculate button
const lengthInput = document.getElementById('length');
const widthInput = document.getElementById('width');
const heightInput = document.getElementById('height');
const quantityInput = document.getElementById('quantity');
const weightInput = document.getElementById('weight');
const calculateBtn = document.getElementById('calculate');
// Get the output label elements
const totalFreightLabel = document.getElementById('totalFreight');
const weightLbLabel = document.getElementById('weightLb');
const seaVolumetricWeightLabel = document.getElementById('seaVolumetricWeight');
const airVolumetricWeightLabel = document.getElementById('airVolumetricWeight');
// Calculate the total freight volume, weight (in pounds), volumetric weight for sea shipment, and volumetric weight for air shipment
function calculate() {
// Get the input values
const length = parseFloat(lengthInput.value);
const width = parseFloat(widthInput.value);
const height = parseFloat(heightInput.value);
const quantity = parseInt(quantityInput.value);
const weight = parseFloat(weightInput.value);
// Calculate the volume (in cubic centimeters)
const volume = length * width * height;
// Calculate the total freight volume (in cubic meters)
const totalVolume = (volume * quantity) / 1000000;
// Calculate the weight (in pounds)
const weightLb = weight * 2.20462;
// Calculate the volumetric weight for sea shipment (in kilograms)
const seaVolumetricWeight = (length * width * height * quantity) / 5000;
// Calculate the volumetric weight for air shipment (in kilograms)
const airVolumetricWeight = (length * width * height * quantity) / 6000;
// Set the output label values
totalFreightLabel.innerText = `${totalVolume.toFixed(2)} cubic meters`;
weightLbLabel.innerText = `${weightLb.toFixed(2)} pounds`;
seaVolumetricWeightLabel.innerText = `${seaVolumetricWeight.toFixed(2)} kilograms`;
airVolumetricWeightLabel.innerText = `${airVolumetricWeight.toFixed(2)} kilograms`;
}
calculateBtn.addEventListener('click', calculate);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment