diff --git a/Conversions/CelsiusToFahrenheit.js b/Conversions/CelsiusToFahrenheit.js new file mode 100644 index 0000000000..e6c3c99520 --- /dev/null +++ b/Conversions/CelsiusToFahrenheit.js @@ -0,0 +1,19 @@ +/** + * @function celsiusToFahrenheit + * @description Converts a temperature from Celsius to Fahrenheit. + * The formula is: F = (C × 9/5) + 32 + * @param {number} celsius - Temperature in Celsius + * @returns {number} - Temperature in Fahrenheit + * @see https://en.wikipedia.org/wiki/Fahrenheit + * @example celsiusToFahrenheit(0) => 32 + * @example celsiusToFahrenheit(100) => 212 + * @example celsiusToFahrenheit(-40) => -40 + */ +const celsiusToFahrenheit = (celsius) => { + if (typeof celsius !== 'number' || isNaN(celsius)) { + throw new TypeError('Input must be a number') + } + return (celsius * 9) / 5 + 32 +} + +export { celsiusToFahrenheit } diff --git a/Conversions/test/CelsiusToFahrenheit.test.js b/Conversions/test/CelsiusToFahrenheit.test.js new file mode 100644 index 0000000000..82efc26bad --- /dev/null +++ b/Conversions/test/CelsiusToFahrenheit.test.js @@ -0,0 +1,35 @@ +import { celsiusToFahrenheit } from '../CelsiusToFahrenheit.js' + +describe('celsiusToFahrenheit', () => { + test('celsiusToFahrenheit(0) returns 32', () => { + expect(celsiusToFahrenheit(0)).toBe(32) + }) + + test('celsiusToFahrenheit(100) returns 212', () => { + expect(celsiusToFahrenheit(100)).toBe(212) + }) + + test('celsiusToFahrenheit(-40) returns -40', () => { + expect(celsiusToFahrenheit(-40)).toBe(-40) + }) + + test('celsiusToFahrenheit(37) returns 98.6', () => { + expect(celsiusToFahrenheit(37)).toBeCloseTo(98.6) + }) + + test('celsiusToFahrenheit(-273.15) returns -459.67', () => { + expect(celsiusToFahrenheit(-273.15)).toBeCloseTo(-459.67) + }) + + test('celsiusToFahrenheit(20) returns 68', () => { + expect(celsiusToFahrenheit(20)).toBe(68) + }) + + test('throws TypeError for non-number input', () => { + expect(() => celsiusToFahrenheit('100')).toThrow(TypeError) + }) + + test('throws TypeError for NaN', () => { + expect(() => celsiusToFahrenheit(NaN)).toThrow(TypeError) + }) +})