diff --git a/adv-math/src/units-conversions/units.js b/adv-math/src/units-conversions/units.js index e769ce2..c1bdad9 100644 --- a/adv-math/src/units-conversions/units.js +++ b/adv-math/src/units-conversions/units.js @@ -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; diff --git a/adv-math/tests/units-conversions.test.js b/adv-math/tests/units-conversions.test.js index 414cbde..81d95b1 100644 --- a/adv-math/tests/units-conversions.test.js +++ b/adv-math/tests/units-conversions.test.js @@ -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 });