Skip to content
Open
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
65 changes: 65 additions & 0 deletions Conversions/SpeedConversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Speed conversion
*
* This function converts speed units including kilometers per hour (km/h),
* meters per second (m/s), miles per hour (mph), and knots (knot).
*
* https://en.wikipedia.org/wiki/Kilometres_per_hour
* https://en.wikipedia.org/wiki/Miles_per_hour
* https://en.wikipedia.org/wiki/Metre_per_second
* https://en.wikipedia.org/wiki/Knot_(unit)
*
* chart for unit conversions.
*
* @constant {Object} speedChart
* @property {number} speedChart['km/h'] - Conversion factor for kilometers per hour (km/h).
* @property {number} speedChart['m/s'] - Conversion factor for meters per second (m/s).
* @property {number} speedChart['mph'] - Conversion factor for miles per hour (mph).
* @property {number} speedChart['knot'] - Conversion factor for knots (knot).
*/
const speedChart = {
'km/h': 1.0,
'm/s': 3.6,
mph: 1.609344,
knot: 1.852
}

/**
* Inverse speed conversion chart for unit conversions.
*
* @constant {Object} speedChartInverse
* @property {number} speedChartInverse['km/h'] - Inverse conversion factor for kilometers per hour (km/h).
* @property {number} speedChartInverse['m/s'] - Inverse conversion factor for meters per second (m/s).
* @property {number} speedChartInverse['mph'] - Inverse conversion factor for miles per hour (mph).
* @property {number} speedChartInverse['knot'] - Inverse conversion factor for knots (knot).
*/
const speedChartInverse = {
'km/h': 1.0,
'm/s': 0.277777778,
mph: 0.621371192,
knot: 0.539956803
}

/**
* Convert speed from one unit to another using the speedChart and speedChartInverse.
*
* @param {number} speed - The speed value to be converted.
* @param {string} inputUnit - The source unit (e.g., 'km/h', 'm/s', 'mph', 'knot').
* @param {string} outputUnit - The target unit (e.g., 'km/h', 'm/s', 'mph', 'knot').
* @throws {Error} Throws an error if the source or target unit is not recognized.
* @returns {number} The converted speed value.
*/
const convertSpeed = (speed, inputUnit, outputUnit) => {
if (!(outputUnit in speedChart) || !(inputUnit in speedChartInverse)) {
const validUnits = Object.keys(speedChartInverse).join(', ')
throw new Error(
`Incorrect 'inputUnit' or 'outputUnit' value: ${inputUnit}, ${outputUnit}\n Valid values are: ${validUnits}`
)
}

return parseFloat(
(speed * speedChart[inputUnit] * speedChartInverse[outputUnit]).toFixed(3)
)
}

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

describe('Speed Unit Conversions', () => {
it('100 km/h to m/s', () => {
expect(convertSpeed(100, 'km/h', 'm/s')).toBe(27.778, 3)
})

it('100 km/h to mph', () => {
expect(convertSpeed(100, 'km/h', 'mph')).toBe(62.137, 3)
})

it('100 km/h to knot', () => {
expect(convertSpeed(100, 'km/h', 'knot')).toBe(53.996, 3)
})

it('100 m/s to km/h', () => {
expect(convertSpeed(100, 'm/s', 'km/h')).toBe(360.0, 3)
})

it('100 m/s to mph', () => {
expect(convertSpeed(100, 'm/s', 'mph')).toBe(223.694, 3)
})

it('100 m/s to knot', () => {
expect(convertSpeed(100, 'm/s', 'knot')).toBe(194.384, 3)
})

it('100 mph to km/h', () => {
expect(convertSpeed(100, 'mph', 'km/h')).toBe(160.934, 3)
})

it('100 mph to m/s', () => {
expect(convertSpeed(100, 'mph', 'm/s')).toBe(44.704, 3)
})

it('100 mph to knot', () => {
expect(convertSpeed(100, 'mph', 'knot')).toBe(86.898, 3)
})

it('100 knot to km/h', () => {
expect(convertSpeed(100, 'knot', 'km/h')).toBe(185.2, 3)
})

it('100 knot to m/s', () => {
expect(convertSpeed(100, 'knot', 'm/s')).toBe(51.444, 3)
})

it('100 knot to mph', () => {
expect(convertSpeed(100, 'knot', 'mph')).toBe(115.078, 3)
})
})