From 9b2abcdf5de9ee18335b999f19144ea331b5b974 Mon Sep 17 00:00:00 2001 From: Cindy Pham <“qdpham@purdue.edu”> Date: Tue, 11 Feb 2025 07:17:17 +0000 Subject: [PATCH] Initial commit --- index.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 13 +++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 index.js create mode 100644 package.json diff --git a/index.js b/index.js new file mode 100644 index 0000000..d9dc87c --- /dev/null +++ b/index.js @@ -0,0 +1,52 @@ +// Helper functions for conversion +function milesToKM(miles) { + return miles * 1.60934; + } + + function kmToMiles(km) { + return km * 0.621371; + } + + function feetToMeters(feet) { + return feet * 0.3048; + } + + function metersToFeet(meters) { + return meters * 3.28084; + } + + // Cloud Function to handle the conversion + exports.convertDistance = (req, res) => { + // Get the unit and value from the query parameters + const unit = req.query.unit; + const value = parseFloat(req.query.value); + + // Check if the value is a valid number + if (isNaN(value)) { + return res.status(400).send('Invalid value'); + } + + let result; + + // Decide which conversion function to call based on the unit + switch (unit) { + case 'miles_to_km': + result = milesToKM(value); + break; + case 'km_to_miles': + result = kmToMiles(value); + break; + case 'feet_to_meters': + result = feetToMeters(value); + break; + case 'meters_to_feet': + result = metersToFeet(value); + break; + default: + return res.status(400).send('Invalid unit'); + } + + // Send the result back as a string (no extra words) + res.status(200).send(result.toString()); + }; + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..68a6153 --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "distance-converter", + "version": "1.0.0", + "description": "A Google Cloud Function to convert distances.", + "main": "index.js", + "dependencies": {}, + "devDependencies": {}, + "scripts": { + "start": "node index.js" + }, + "author": "Your Name", + "license": "ISC" +}