File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 }
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments