Using HTML and Tailwind v3, you need to write a script to create a web application:
Create a tool that will calculate the number of steps taken by a pedestrian
Assistant
Certainly! Below is an HTML script that uses Tailwind CSS v3 to create a web application for calculating the number of steps taken by a pedestrian. This tool allows users to input the total distance they've walked and their individual step length to compute the total number of steps.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pedestrian Step Calculator</title>
<!-- Include Tailwind CSS via CDN -->
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
<div class="bg-white p-8 rounded shadow-md w-full max-w-md">
<h1 class="text-2xl font-bold mb-6 text-center">Pedestrian Step Calculator</h1>
<form id="stepForm" class="space-y-4">
<!-- Distance Input -->
<div>
<label for="distance" class="block text-gray-700">Total Distance Walked</label>
<div class="mt-1 flex">
<input type="number" id="distance" name="distance" class="w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500" placeholder="Enter distance">
<select id="distanceUnit" name="distanceUnit" class="ml-2 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500">
<option value="kilometers">Kilometers</option>
<option value="miles">Miles</option>
</select>
</div>
</div>
<!-- Step Length Input -->
<div>
<label for="strideLength" class="block text-gray-700">Your Step Length</label>
<div class="mt-1 flex">
<input type="number" id="strideLength" name="strideLength" class="w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500" placeholder="Enter step length">
<select id="strideUnit" name="strideUnit" class="ml-2 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500">
<option value="meters">Meters</option>
<option value="feet">Feet</option>
</select>
</div>
</div>
<!-- Calculate Button -->
<button type="button" onclick="calculateSteps()" class="w-full bg-indigo-600 text-white py-2 px-4 rounded-md hover:bg-indigo-700">Calculate Steps</button>
</form>
<!-- Result Display -->
<div id="result" class="mt-6 text-center text-xl font-semibold text-gray-800"></div>
</div>
<!-- Calculation Script -->
<script>
function calculateSteps() {
// Retrieve the distance value and its unit
let distance = parseFloat(document.getElementById('distance').value);
let distanceUnit = document.getElementById('distanceUnit').value;
// Retrieve the stride length value and its unit
let strideLength = parseFloat(document.getElementById('strideLength').value);
let strideUnit = document.getElementById('strideUnit').value;
// Validate the inputs
if (isNaN(distance) || distance <= 0) {
alert('Please enter a valid distance.');
return;
}
if (isNaN(strideLength) || strideLength <= 0) {
alert('Please enter a valid step length.');
return;
}
// Convert distance to meters
if (distanceUnit === 'kilometers') {
distance = distance * 1000; // Convert kilometers to meters
} else if (distanceUnit === 'miles') {
distance = distance * 1609.34; // Convert miles to meters
}
// Convert stride length to meters
if (strideUnit === 'feet') {
strideLength = strideLength * 0.3048; // Convert feet to meters
}
// If strideUnit is meters, no conversion needed
// Calculate the number of steps
let steps = distance / strideLength;
steps = Math.round(steps);
// Display the result
document.getElementById('result').innerText = `Total Steps Taken: ${steps}`;
}
</script>
</body>
</html>
How the Code Works
HTML Structure: The document is structured with a simple and clean layout. It includes a form where users can input their total distance walked and their step length.
Tailwind CSS Integration:
The script tag <script src="https://cdn.tailwindcss.com"></script> includes Tailwind CSS v3 via CDN.
Tailwind utility classes are used throughout the HTML to style elements, ensuring a responsive and modern design without writing custom CSS.
Form Elements:
Distance Input:
An input field for entering the total distance walked.
A dropdown to select the unit of distance (kilometers or miles).
Step Length Input:
An input field for entering the user's step length.
A dropdown to select the unit of step length (meters or feet).
Calculate Button:
A button that triggers the calculateSteps() function when clicked.
JavaScript Functionality:
The calculateSteps() function performs the calculation when the button is clicked.
Input Retrieval and Validation:
Retrieves values from the input fields.
Checks if the inputs are valid numbers greater than zero.
Alerts the user if invalid inputs are detected.
Unit Conversion:
Converts distance to meters:
Kilometers to meters: distance * 1000.
Miles to meters: distance * 1609.34.
Converts stride length to meters:
Feet to meters: strideLength * 0.3048.
If already in meters, no conversion is necessary.
Calculation:
Calculates the number of steps using the formula:
steps = distance / strideLength
Uses Math.round() to round the result to the nearest whole number.
Result Display:
Displays the total number of steps in the <div id="result"> element.
Usage Instructions
Open the HTML File:
Save the code above into a file with an .html extension (e.g., step-calculator.html).
Open the file in a modern web browser (e.g., Chrome, Firefox).
Enter Your Data:
Total Distance Walked:
Input the total distance you've walked.
Select the appropriate unit (kilometers or miles).
Your Step Length:
Input your average step length.
Select the appropriate unit (meters or feet).
Note: If you're unsure of your step length, an average adult step length is approximately 0.762 meters (2.5 feet).
Calculate Steps:
Click the Calculate Steps button.
The total number of steps taken will be displayed below the form.
Customization
Styling:
You can customize the styling by modifying the Tailwind classes or adding new ones.
For more advanced styling, you can set up a Tailwind CSS build process to enable features like custom colors or themes.
Functionality:
The calculation can be adjusted if you have a different formula or want to account for additional factors like terrain or walking speed.
Additional input validation or error handling can be added for robustness.
Additional Notes
Accuracy:
The accuracy of the step count depends on the precision of the distance and step length inputs.
Encourage users to measure their step length over several steps for a more accurate average.
Compatibility:
Ensure JavaScript is enabled in the browser for the calculation function to work.
The Tailwind CDN approach is suitable for small projects or demos. For larger applications, it's recommended to install Tailwind via npm and configure it with a build tool.
By using this script, you now have a functional web application that calculates the number of steps taken based on user inputs for distance and step length, all styled with Tailwind CSS v3 for a sleek and responsive design.