CODE FOR WEIGHT CONVERTOR

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Weight Unit Converter</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            height: 100vh;
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        .converter {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            transition: transform 0.2s;
        }
        .converter:hover {
            transform: scale(1.02);
        }
        select, input[type="number"], button {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 15px;
            font-weight: bold;
        }
    </style>
</head>
<body>

<h1>Weight Unit Converter</h1>
<div class="converter">
    <input type="number" id="weightInput" placeholder="Enter weight" required>
    <select id="fromUnit">
        <option value="g">Grams (g)</option>
        <option value="kg">Kilograms (kg)</option>
        <option value="lb">Pounds (lb)</option>
        <option value="oz">Ounces (oz)</option>
    </select>
    <select id="toUnit">
        <option value="g">Grams (g)</option>
        <option value="kg">Kilograms (kg)</option>
        <option value="lb">Pounds (lb)</option>
        <option value="oz">Ounces (oz)</option>
    </select>
    <button onclick="convertWeight()">Convert</button>
    <div class="result" id="result"></div>
</div>

<script>
    function convertWeight() {
        const weight = parseFloat(document.getElementById("weightInput").value);
        const fromUnit = document.getElementById("fromUnit").value;
        const toUnit = document.getElementById("toUnit").value;
        let result;

        if (isNaN(weight)) {
            document.getElementById("result").innerText = "Please enter a valid weight.";
            return;
        }

        // Conversion logic
        const conversions = {
            "g": {
                "kg": weight / 1000,
                "lb": weight * 0.00220462,
                "oz": weight * 0.035274,
            },
            "kg": {
                "g": weight * 1000,
                "lb": weight * 2.20462,
                "oz": weight * 35.274,
            },
            "lb": {
                "g": weight / 0.00220462,
                "kg": weight / 2.20462,
                "oz": weight * 16,
            },
            "oz": {
                "g": weight / 0.035274,
                "kg": weight / 35.274,
                "lb": weight / 16,
            }
        };

        result = conversions[fromUnit][toUnit];
        document.getElementById("result").innerText = `Result: ${result.toFixed(2)} ${toUnit}`;
    }
</script>

</body>
</html>
HTML

Leave a Reply

Your email address will not be published. Required fields are marked *

Resize text
Scroll to Top