Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6291d4b

Browse files
feat: Add Length Conversion (TheAlgorithms#1390)
1 parent 0604d06 commit 6291d4b

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

‎Conversions/LengthConversion.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Converts a length from one unit to another.
3+
*
4+
* @param {number} length - The length to convert.
5+
* @param {string} fromUnit - The unit to convert from (e.g., "km", "m", "cm").
6+
* @param {string} toUnit - The unit to convert to (e.g., "km", "m", "cm").
7+
* @returns {number} The converted length.
8+
* @throws {Error} If the units are invalid or not found in the conversion dictionary.
9+
*/
10+
11+
const lengthConversion = (length, fromUnit, toUnit) => {
12+
// Define a dictionary to map units to meters
13+
const meters = {
14+
mm: 0.001,
15+
cm: 0.01,
16+
m: 1,
17+
km: 1000,
18+
inch: 0.0254,
19+
ft: 0.3048,
20+
yd: 0.9144,
21+
mi: 1609.34
22+
}
23+
24+
// Check if the units are in the dictionary, otherwise, throw an error
25+
if (!(fromUnit in meters) || !(toUnit in meters)) {
26+
throw new Error('Invalid units')
27+
}
28+
29+
// Perform the conversion
30+
const metersFrom = length * meters[fromUnit]
31+
const convertedLength = metersFrom / meters[toUnit]
32+
33+
return convertedLength
34+
}
35+
36+
export {
37+
lengthConversion
38+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { lengthConversion } from '../LengthConversion.js'
2+
3+
describe('LengthConversion', () => {
4+
it.each`
5+
length | fromUnit | toUnit | expected
6+
${10} | ${'km'} | ${'m'} | ${10000}
7+
${100} | ${'m'} | ${'km'} | ${0.1}
8+
${5} | ${'cm'} | ${'mm'} | ${50}
9+
${12} | ${'ft'} | ${'inch'}| ${144.00000000000003}
10+
`(
11+
'converts $length $fromUnit to $toUnit',
12+
({ length, fromUnit, toUnit, expected }) => {
13+
try {
14+
const result = lengthConversion(length, fromUnit, toUnit)
15+
expect(result).toBe(expected)
16+
} catch (error) {
17+
expect(error).toBeUndefined()
18+
}
19+
}
20+
)
21+
22+
it.each`
23+
length | fromUnit | toUnit | expected
24+
${10} | ${'m'} | ${'km'} | ${0.01}
25+
${1000}| ${'mm'} | ${'cm'} | ${100}
26+
${1} | ${'inch'}| ${'ft'} | ${0.08333333333}
27+
`(
28+
'converts $length $fromUnit to $toUnit (vice versa)',
29+
({ length, fromUnit, toUnit, expected }) => {
30+
try {
31+
const result = lengthConversion(length, fromUnit, toUnit)
32+
expect(result).toBeCloseTo(expected, 10) // Close comparison due to floating-point precision
33+
} catch (error) {
34+
expect(error).toBeUndefined()
35+
}
36+
}
37+
)
38+
39+
it.each`
40+
length | fromUnit | toUnit | expectedError
41+
${10} | ${'km'} | ${'invalid'} | ${'Invalid units'}
42+
${5} | ${'invalid'} | ${'m'} | ${'Invalid units'}
43+
`(
44+
'returns error message for invalid units: $fromUnit to $toUnit',
45+
({ length, fromUnit, toUnit, expectedError }) => {
46+
try {
47+
lengthConversion(length, fromUnit, toUnit)
48+
} catch (error) {
49+
expect(error.message).toBe(expectedError)
50+
}
51+
}
52+
)
53+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /