-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matthew Sanner
committed
Oct 6, 2023
1 parent
6d20280
commit f906564
Showing
2 changed files
with
71 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,33 @@ | ||
class Units { | ||
// Method to convert length units (e.g., meters to feet) | ||
static convertLength(value, fromUnit, toUnit) { | ||
const conversions = { | ||
meters: 3.28084, // 1 meter = 3.28084 feet | ||
feet: 0.3048, // 1 foot = 0.3048 meters | ||
// Add more unit conversions as needed | ||
}; | ||
|
||
if (!conversions[fromUnit] || !conversions[toUnit]) { | ||
throw new Error('Invalid unit(s).'); | ||
} | ||
|
||
return value * conversions[fromUnit] / conversions[toUnit]; | ||
// Method to convert length units (e.g., meters to feet) | ||
static convertLength(value, fromUnit, toUnit) { | ||
if (fromUnit === "meters" && toUnit === "feet") { | ||
return value * 3.28084; | ||
} else if (fromUnit === "feet" && toUnit === "meters") { | ||
return value * 0.3048; | ||
} else { | ||
throw new Error("Invalid length unit conversion."); | ||
} | ||
} | ||
|
||
// Method to convert temperature units (e.g., Celsius to Fahrenheit) | ||
static convertTemperature(value, fromUnit, toUnit) { | ||
if (fromUnit === 'Celsius' && toUnit === 'Fahrenheit') { | ||
return (value * 9 / 5) + 32; | ||
} else if (fromUnit=='Celsius' && toUnit=='Kelvin') { | ||
return value + 273.15; | ||
} else if (fromUnit === 'Fahrenheit' && toUnit === 'Celsius') { | ||
return (value - 32) * 5 / 9; | ||
} else if (fromUnit=='Fahrenheit' && toUnit=='Kelvin') { | ||
return (value + 459.67) * 5 / 9; | ||
} else if (fromUnit=='Kelvin' && toUnit=='Celsius') { | ||
return value - 273.15; | ||
} else if (fromUnit=='Kelvin' && toUnit=='Fahrenheit') { | ||
return (value * 9 / 5) - 459.67; | ||
} | ||
|
||
else { | ||
throw new Error('Invalid temperature unit conversion.'); | ||
} | ||
// Method to convert temperature units (e.g., Celsius to Fahrenheit) | ||
static convertTemperature(value, fromUnit, toUnit) { | ||
if (fromUnit === "Celsius" && toUnit === "Fahrenheit") { | ||
return (value * 9) / 5 + 32; | ||
} else if (fromUnit == "Celsius" && toUnit == "Kelvin") { | ||
return value + 273.15; | ||
} else if (fromUnit === "Fahrenheit" && toUnit === "Celsius") { | ||
return ((value - 32) * 5) / 9; | ||
} else if (fromUnit == "Fahrenheit" && toUnit == "Kelvin") { | ||
return ((value + 459.67) * 5) / 9; | ||
} else if (fromUnit == "Kelvin" && toUnit == "Celsius") { | ||
return value - 273.15; | ||
} else if (fromUnit == "Kelvin" && toUnit == "Fahrenheit") { | ||
return (value * 9) / 5 - 459.67; | ||
} else { | ||
throw new Error("Invalid temperature unit conversion."); | ||
} | ||
} | ||
} | ||
|
||
module.exports = Units; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,50 @@ | ||
const Units = require('../src/units-conversions/units'); | ||
const Units = require("../src/units-conversions/units"); | ||
|
||
describe('Units', () => { | ||
test('converts meters to feet', () => { | ||
expect(Units.convertLength(5, 'meters', 'feet')).toBeCloseTo(53.8195, 3); | ||
}); | ||
describe("Units", () => { | ||
test("converts meters to feet", () => { | ||
expect(Units.convertLength(5, "meters", "feet")).toBeCloseTo(16.4042, 3); | ||
}); | ||
|
||
test('converts feet to meters', () => { | ||
expect(Units.convertLength(10, 'feet', 'meters')).toBeCloseTo(0.929, 3); | ||
}); | ||
test("converts feet to meters", () => { | ||
expect(Units.convertLength(10, "feet", "meters")).toBeCloseTo(3.048, 2); | ||
}); | ||
|
||
test('converts Celsius to Fahrenheit', () => { | ||
expect(Units.convertTemperature(25, 'Celsius', 'Fahrenheit')).toBeCloseTo(77, 1); | ||
}); | ||
test('converts Celsius to Kelvin', () => { | ||
expect(Units.convertTemperature(25, 'Celsius', 'Kelvin')).toBeCloseTo(298.15, 1); | ||
}); | ||
test('converts Fahrenheit to Kelvin', () => { | ||
expect(Units.convertTemperature(68, 'Fahrenheit', 'Kelvin')).toBeCloseTo(293.15, 1); | ||
}); | ||
test('converts Fahrenheit to Celsius', () => { | ||
expect(Units.convertTemperature(68, 'Fahrenheit', 'Celsius')).toBeCloseTo(20, 1); | ||
}); | ||
test('converts Kelvin to Celsius', () => { | ||
expect(Units.convertTemperature(300, 'Kelvin', 'Celsius')).toBeCloseTo(26.85, 1); | ||
}); | ||
test('converts Kelvin to Fahrenheit', () => { | ||
expect(Units.convertTemperature(300, 'Kelvin', 'Fahrenheit')).toBeCloseTo(80.33, 1); | ||
}); | ||
test("converts Celsius to Fahrenheit", () => { | ||
expect(Units.convertTemperature(25, "Celsius", "Fahrenheit")).toBeCloseTo( | ||
77, | ||
1 | ||
); | ||
}); | ||
test("converts Celsius to Kelvin", () => { | ||
expect(Units.convertTemperature(25, "Celsius", "Kelvin")).toBeCloseTo( | ||
298.15, | ||
1 | ||
); | ||
}); | ||
test("converts Fahrenheit to Kelvin", () => { | ||
expect(Units.convertTemperature(68, "Fahrenheit", "Kelvin")).toBeCloseTo( | ||
293.15, | ||
1 | ||
); | ||
}); | ||
test("converts Fahrenheit to Celsius", () => { | ||
expect(Units.convertTemperature(68, "Fahrenheit", "Celsius")).toBeCloseTo( | ||
20, | ||
1 | ||
); | ||
}); | ||
test("converts Kelvin to Celsius", () => { | ||
expect(Units.convertTemperature(300, "Kelvin", "Celsius")).toBeCloseTo( | ||
26.85, | ||
1 | ||
); | ||
}); | ||
test("converts Kelvin to Fahrenheit", () => { | ||
expect(Units.convertTemperature(300, "Kelvin", "Fahrenheit")).toBeCloseTo( | ||
80.33, | ||
1 | ||
); | ||
}); | ||
|
||
// Add more test cases as needed | ||
// Add more test cases as needed | ||
}); |