Skip to content

Commit 7d602f2

Browse files
feat: add Celsius to Fahrenheit conversion
1 parent 5c39e87 commit 7d602f2

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎Conversions/CelsiusToFahrenheit.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @function celsiusToFahrenheit
3+
* @description Converts a temperature from Celsius to Fahrenheit.
4+
* The formula is: F = (C × 9/5) + 32
5+
* @param {number} celsius - Temperature in Celsius
6+
* @returns {number} - Temperature in Fahrenheit
7+
* @see https://en.wikipedia.org/wiki/Fahrenheit
8+
* @example celsiusToFahrenheit(0) => 32
9+
* @example celsiusToFahrenheit(100) => 212
10+
* @example celsiusToFahrenheit(-40) => -40
11+
*/
12+
const celsiusToFahrenheit = (celsius) => {
13+
if (typeof celsius !== 'number' || isNaN(celsius)) {
14+
throw new TypeError('Input must be a number')
15+
}
16+
return (celsius * 9) / 5 + 32
17+
}
18+
19+
export { celsiusToFahrenheit }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { celsiusToFahrenheit } from '../CelsiusToFahrenheit.js'
2+
3+
describe('celsiusToFahrenheit', () => {
4+
test('celsiusToFahrenheit(0) returns 32', () => {
5+
expect(celsiusToFahrenheit(0)).toBe(32)
6+
})
7+
8+
test('celsiusToFahrenheit(100) returns 212', () => {
9+
expect(celsiusToFahrenheit(100)).toBe(212)
10+
})
11+
12+
test('celsiusToFahrenheit(-40) returns -40', () => {
13+
expect(celsiusToFahrenheit(-40)).toBe(-40)
14+
})
15+
16+
test('celsiusToFahrenheit(37) returns 98.6', () => {
17+
expect(celsiusToFahrenheit(37)).toBeCloseTo(98.6)
18+
})
19+
20+
test('celsiusToFahrenheit(-273.15) returns -459.67', () => {
21+
expect(celsiusToFahrenheit(-273.15)).toBeCloseTo(-459.67)
22+
})
23+
24+
test('celsiusToFahrenheit(20) returns 68', () => {
25+
expect(celsiusToFahrenheit(20)).toBe(68)
26+
})
27+
28+
test('throws TypeError for non-number input', () => {
29+
expect(() => celsiusToFahrenheit('100')).toThrow(TypeError)
30+
})
31+
32+
test('throws TypeError for NaN', () => {
33+
expect(() => celsiusToFahrenheit(NaN)).toThrow(TypeError)
34+
})
35+
})

0 commit comments

Comments
 (0)