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
19 changes: 19 additions & 0 deletions Conversions/CelsiusToFahrenheit.js
Original file line number Diff line number Diff line change
@@ -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 }
35 changes: 35 additions & 0 deletions Conversions/test/CelsiusToFahrenheit.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
Loading