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 fahrenheitToCelsius
3+ * @description Converts a temperature from Fahrenheit to Celsius.
4+ * The formula is: C = (F - 32) × 5/9
5+ * @param {number } fahrenheit - Temperature in Fahrenheit
6+ * @returns {number } - Temperature in Celsius
7+ * @see https://en.wikipedia.org/wiki/Celsius
8+ * @example fahrenheitToCelsius(32) => 0
9+ * @example fahrenheitToCelsius(212) => 100
10+ * @example fahrenheitToCelsius(-40) => -40
11+ */
12+ const fahrenheitToCelsius = ( fahrenheit ) => {
13+ if ( typeof fahrenheit !== 'number' || isNaN ( fahrenheit ) ) {
14+ throw new TypeError ( 'Input must be a number' )
15+ }
16+ return ( ( fahrenheit - 32 ) * 5 ) / 9
17+ }
18+
19+ export { fahrenheitToCelsius }
Original file line number Diff line number Diff line change 1+ import { fahrenheitToCelsius } from '../FahrenheitToCelsius.js'
2+
3+ describe ( 'fahrenheitToCelsius' , ( ) => {
4+ test ( 'fahrenheitToCelsius(32) returns 0' , ( ) => {
5+ expect ( fahrenheitToCelsius ( 32 ) ) . toBe ( 0 )
6+ } )
7+
8+ test ( 'fahrenheitToCelsius(212) returns 100' , ( ) => {
9+ expect ( fahrenheitToCelsius ( 212 ) ) . toBe ( 100 )
10+ } )
11+
12+ test ( 'fahrenheitToCelsius(-40) returns -40' , ( ) => {
13+ expect ( fahrenheitToCelsius ( - 40 ) ) . toBe ( - 40 )
14+ } )
15+
16+ test ( 'fahrenheitToCelsius(98.6) returns 37' , ( ) => {
17+ expect ( fahrenheitToCelsius ( 98.6 ) ) . toBeCloseTo ( 37 )
18+ } )
19+
20+ test ( 'fahrenheitToCelsius(68) returns 20' , ( ) => {
21+ expect ( fahrenheitToCelsius ( 68 ) ) . toBeCloseTo ( 20 )
22+ } )
23+
24+ test ( 'fahrenheitToCelsius(-459.67) returns -273.15' , ( ) => {
25+ expect ( fahrenheitToCelsius ( - 459.67 ) ) . toBeCloseTo ( - 273.15 )
26+ } )
27+
28+ test ( 'throws TypeError for non-number input' , ( ) => {
29+ expect ( ( ) => fahrenheitToCelsius ( '32' ) ) . toThrow ( TypeError )
30+ } )
31+
32+ test ( 'throws TypeError for NaN' , ( ) => {
33+ expect ( ( ) => fahrenheitToCelsius ( NaN ) ) . toThrow ( TypeError )
34+ } )
35+ } )
You can’t perform that action at this time.
0 commit comments