Skip to content

Commit 25c9761

Browse files
committed
feat:added SpeedConversion.js
1 parent 3423829 commit 25c9761

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

Conversions/SpeedConversion.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Speed conversion
3+
*
4+
* chart for unit conversions.
5+
*
6+
* @constant {Object} speedChart
7+
* @property {number} speedChart['km/h'] - Conversion factor for kilometers per hour (km/h).
8+
* @property {number} speedChart['m/s'] - Conversion factor for meters per second (m/s).
9+
* @property {number} speedChart['mph'] - Conversion factor for miles per hour (mph).
10+
* @property {number} speedChart['knot'] - Conversion factor for knots (knot).
11+
*/
12+
const speedChart = {
13+
'km/h': 1.0,
14+
'm/s': 3.6,
15+
mph: 1.609344,
16+
knot: 1.852
17+
}
18+
19+
/**
20+
* Inverse speed conversion chart for unit conversions.
21+
*
22+
* @constant {Object} speedChartInverse
23+
* @property {number} speedChartInverse['km/h'] - Inverse conversion factor for kilometers per hour (km/h).
24+
* @property {number} speedChartInverse['m/s'] - Inverse conversion factor for meters per second (m/s).
25+
* @property {number} speedChartInverse['mph'] - Inverse conversion factor for miles per hour (mph).
26+
* @property {number} speedChartInverse['knot'] - Inverse conversion factor for knots (knot).
27+
*/
28+
const speedChartInverse = {
29+
'km/h': 1.0,
30+
'm/s': 0.277777778,
31+
mph: 0.621371192,
32+
knot: 0.539956803
33+
}
34+
35+
/**
36+
* Convert speed from one unit to another using the speedChart and speedChartInverse.
37+
*
38+
* @param {number} speed - The speed value to be converted.
39+
* @param {string} inputUnit - The source unit (e.g., 'km/h', 'm/s', 'mph', 'knot').
40+
* @param {string} outputUnit - The target unit (e.g., 'km/h', 'm/s', 'mph', 'knot').
41+
* @throws {Error} Throws an error if the source or target unit is not recognized.
42+
* @returns {number} The converted speed value.
43+
*/
44+
const convertSpeed = (speed, inputUnit, outputUnit) => {
45+
if (!(outputUnit in speedChart) || !(inputUnit in speedChartInverse)) {
46+
const validUnits = Object.keys(speedChartInverse).join(', ')
47+
throw new Error(
48+
`Incorrect 'inputUnit' or 'outputUnit' value: ${inputUnit}, ${outputUnit}\nValid values are: ${validUnits}`
49+
)
50+
}
51+
52+
return parseFloat(
53+
(speed * speedChart[inputUnit] * speedChartInverse[outputUnit]).toFixed(3)
54+
)
55+
}
56+
57+
export { convertSpeed }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { convertSpeed } from '../SpeedConversion'
2+
3+
describe('Speed Unit Conversions', () => {
4+
it('100 km/h to m/s', () => {
5+
expect(convertSpeed(100, 'km/h', 'm/s')).toBe(27.778, 3)
6+
})
7+
8+
it('100 km/h to mph', () => {
9+
expect(convertSpeed(100, 'km/h', 'mph')).toBe(62.137, 3)
10+
})
11+
12+
it('100 km/h to knot', () => {
13+
expect(convertSpeed(100, 'km/h', 'knot')).toBe(53.996, 3)
14+
})
15+
16+
it('100 m/s to km/h', () => {
17+
expect(convertSpeed(100, 'm/s', 'km/h')).toBe(360.0, 3)
18+
})
19+
20+
it('100 m/s to mph', () => {
21+
expect(convertSpeed(100, 'm/s', 'mph')).toBe(223.694, 3)
22+
})
23+
24+
it('100 m/s to knot', () => {
25+
expect(convertSpeed(100, 'm/s', 'knot')).toBe(194.384, 3)
26+
})
27+
28+
it('100 mph to km/h', () => {
29+
expect(convertSpeed(100, 'mph', 'km/h')).toBe(160.934, 3)
30+
})
31+
32+
it('100 mph to m/s', () => {
33+
expect(convertSpeed(100, 'mph', 'm/s')).toBe(44.704, 3)
34+
})
35+
36+
it('100 mph to knot', () => {
37+
expect(convertSpeed(100, 'mph', 'knot')).toBe(86.898, 3)
38+
})
39+
40+
it('100 knot to km/h', () => {
41+
expect(convertSpeed(100, 'knot', 'km/h')).toBe(185.2, 3)
42+
})
43+
44+
it('100 knot to m/s', () => {
45+
expect(convertSpeed(100, 'knot', 'm/s')).toBe(51.444, 3)
46+
})
47+
48+
it('100 knot to mph', () => {
49+
expect(convertSpeed(100, 'knot', 'mph')).toBe(115.078, 3)
50+
})
51+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy