Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Length Conversion #1390

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Conversions/LengthConversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Converts a length from one unit to another.
*
* @param {number} length - The length to convert.
* @param {string} fromUnit - The unit to convert from (e.g., "km", "m", "cm").
* @param {string} toUnit - The unit to convert to (e.g., "km", "m", "cm").
* @returns {number} The converted length.
* @throws {Error} If the units are invalid or not found in the conversion dictionary.
*/

const lengthConversion = (length, fromUnit, toUnit) => {
// Define a dictionary to map units to meters
const meters = {
mm: 0.001,
cm: 0.01,
m: 1,
km: 1000,
inch: 0.0254,
ft: 0.3048,
yd: 0.9144,
mi: 1609.34
}

// Check if the units are in the dictionary, otherwise, throw an error
if (!(fromUnit in meters) || !(toUnit in meters)) {
throw new Error('Invalid units')
}

// Perform the conversion
const metersFrom = length * meters[fromUnit]
const convertedLength = metersFrom / meters[toUnit]

return convertedLength
}

export {
lengthConversion
}
53 changes: 53 additions & 0 deletions Conversions/test/LengthConversion.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { lengthConversion } from '../LengthConversion.js'

describe('LengthConversion', () => {
it.each`
length | fromUnit | toUnit | expected
${10} | ${'km'} | ${'m'} | ${10000}
${100} | ${'m'} | ${'km'} | ${0.1}
${5} | ${'cm'} | ${'mm'} | ${50}
${12} | ${'ft'} | ${'inch'}| ${144.00000000000003}
`(
'converts $length $fromUnit to $toUnit',
({ length, fromUnit, toUnit, expected }) => {
try {
const result = lengthConversion(length, fromUnit, toUnit)
expect(result).toBe(expected)
} catch (error) {
expect(error).toBeUndefined()
}
}
)

it.each`
length | fromUnit | toUnit | expected
${10} | ${'m'} | ${'km'} | ${0.01}
${1000}| ${'mm'} | ${'cm'} | ${100}
${1} | ${'inch'}| ${'ft'} | ${0.08333333333}
`(
'converts $length $fromUnit to $toUnit (vice versa)',
({ length, fromUnit, toUnit, expected }) => {
try {
const result = lengthConversion(length, fromUnit, toUnit)
expect(result).toBeCloseTo(expected, 10) // Close comparison due to floating-point precision
} catch (error) {
expect(error).toBeUndefined()
}
}
)

it.each`
length | fromUnit | toUnit | expectedError
${10} | ${'km'} | ${'invalid'} | ${'Invalid units'}
${5} | ${'invalid'} | ${'m'} | ${'Invalid units'}
`(
'returns error message for invalid units: $fromUnit to $toUnit',
({ length, fromUnit, toUnit, expectedError }) => {
try {
lengthConversion(length, fromUnit, toUnit)
} catch (error) {
expect(error.message).toBe(expectedError)
}
}
)
})