Skip to content

Commit 124d201

Browse files
chore: merge Added Temperature Conversions to Javascript (TheAlgorithms#705)
* Added Temperature Conversions to Javascript I added a file which contains essential conversions needed to swap temperature units to one another. Hope you like it :) * Update Conversions/TemperatureConversion.js Suggestions saved Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com> * Added test file "test" folder was created and tests required for "TemperatureConversions" was added to it * Fixed minor syntax errors * Update Conversions/test/TemperatureConversion.test.js Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com> * Formatted TemperatureConversion.js The code was modified to comply with rules and styes recommended by the repository * Formatted TemperatureConversion.test.js The code was modified to comply with rules and styes recommended by the repository * Reformatted :) Hope this passes the test :) Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com>
1 parent 95b75e8 commit 124d201

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed

Conversions/TemperatureConversion.js

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// This files has functions to convert different temperature units
2+
// Functions take temperature value as a arguement and returns corresponding converted value
3+
4+
const celsiusToFahrenheit = (celsius) => {
5+
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
6+
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
7+
return Math.round(((celsius) * 9 / 5) + 32)
8+
}
9+
10+
const celsiusToKelvin = (celsius) => {
11+
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
12+
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
13+
return Math.round((celsius) + 273.15)
14+
}
15+
16+
const celsiusToRankine = (celsius) => {
17+
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
18+
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
19+
return Math.round(((celsius) * 9 / 5) + 491.67)
20+
}
21+
22+
const fahrenheitToCelsius = (fahrenheit) => {
23+
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
24+
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
25+
return Math.round(((fahrenheit) - 32) * 5 / 9)
26+
}
27+
28+
const fahrenheitToKelvin = (fahrenheit) => {
29+
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
30+
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
31+
return Math.round((((fahrenheit) - 32) * 5 / 9) + 273.15)
32+
}
33+
34+
const fahrenheitToRankine = (fahrenheit) => {
35+
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
36+
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
37+
return Math.round((fahrenheit) + 459.67)
38+
}
39+
40+
const kelvinToCelsius = (kelvin) => {
41+
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
42+
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
43+
return Math.round((kelvin) - 273.15)
44+
45+
}
46+
47+
const kelvinToFahrenheit = (kelvin) => {
48+
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
49+
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
50+
return Math.round((((kelvin) - 273.15) * 9 / 5) + 32)
51+
}
52+
53+
const kelvinToRankine = (kelvin) => {
54+
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
55+
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
56+
return Math.round(( (kelvin) * 9 / 5))
57+
}
58+
59+
const rankineToCelsius = (rankine) => {
60+
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
61+
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
62+
return Math.round(((rankine) - 491.67) * 5 / 9)
63+
}
64+
65+
const rankineToFahrenheit = (rankine) => {
66+
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
67+
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
68+
return Math.round((rankine) - 459.67)
69+
}
70+
71+
const rankineToKelvin = (rankine) => {
72+
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
73+
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
74+
return Math.round(((rankine) * 5 / 9))
75+
}
76+
77+
const reaumurToKelvin = (reaumur) => {
78+
// Reference:- http://www.csgnetwork.com/temp2conv.html
79+
return Math.round(((reaumur) * 1.25 + 273.15))
80+
}
81+
82+
const reaumurToFahrenheit = (reaumur) => {
83+
// Reference:- http://www.csgnetwork.com/temp2conv.html
84+
return Math.round(((reaumur) * 2.25 + 32))
85+
}
86+
87+
const reaumurToCelsius = (reaumur) => {
88+
// Reference:- http://www.csgnetwork.com/temp2conv.html
89+
return Math.round(((reaumur) * 1.25))
90+
}
91+
92+
const reaumurToRankine = (reaumur) => {
93+
// Reference:- http://www.csgnetwork.com/temp2conv.html
94+
return Math.round(((reaumur) * 2.25 + 32 + 459.67))
95+
}
96+
97+
export {
98+
celsiusToFahrenheit, celsiusToKelvin, celsiusToRankine,
99+
fahrenheitToCelsius, fahrenheitToKelvin, fahrenheitToRankine,
100+
kelvinToCelsius, kelvinToFahrenheit, kelvinToRankine,
101+
rankineToCelsius, rankineToFahrenheit, rankineToKelvin,
102+
reaumurToCelsius, reaumurToFahrenheit, reaumurToKelvin, reaumurToRankine
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import * as tc from '../TemperatureConversion.js'
2+
3+
describe('Testing Conversion of Celsius to fahrenheit', () => {
4+
it('with celsius value', () => {
5+
const test1 = tc.celsiusToFahrenheit(10)
6+
expect(test1).toBe(50)
7+
})
8+
})
9+
10+
describe('Testing Conversion of Celsius to kelvin', () => {
11+
it('with celsius value', () => {
12+
const test1 = tc.celsiusToKelvin(15)
13+
expect(test1).toBe(288)
14+
})
15+
})
16+
17+
describe('Testing Conversion of Celsius to Rankine', () => {
18+
it('with celsius value', () => {
19+
const test1 = tc.celsiusToRankine(28)
20+
expect(test1).toBe(542)
21+
})
22+
})
23+
24+
describe('Testing Conversion of Fahrenheit to Celsius', () => {
25+
it('with Fahrenheit value', () => {
26+
const test1 = tc.fahrenheitToCelsius(134)
27+
expect(test1).toBe(57)
28+
})
29+
})
30+
31+
describe('Testing Conversion of Fahrenheit to Kelvin', () => {
32+
it('with Fahrenheit value', () => {
33+
const test1 = tc.fahrenheitToKelvin(125)
34+
expect(test1).toBe(325)
35+
})
36+
})
37+
38+
describe('Testing Conversion of Fahrenheit to Rankine', () => {
39+
it('with Fahrenheit value', () => {
40+
const test1 = tc.fahrenheitToRankine(10)
41+
expect(test1).toBe(470)
42+
})
43+
})
44+
45+
describe('Testing Conversion of Kelvin to Celsius', () => {
46+
it('with Kelvin value', () => {
47+
const test1 = tc.kelvinToCelsius(100)
48+
expect(test1).toBe(-173)
49+
})
50+
})
51+
52+
describe('Testing Conversion of Kelvin to Fahrenheit', () => {
53+
it('with Kelvin value', () => {
54+
const test1 = tc.kelvinToFahrenheit(20)
55+
expect(test1).toBe(-424)
56+
})
57+
})
58+
59+
describe('Testing Conversion of Kelvin to Rankine', () => {
60+
it('with kelvin value', () => {
61+
const test1 = tc.kelvinToRankine(69)
62+
expect(test1).toBe(124)
63+
})
64+
})
65+
describe('Testing Conversion of Rankine to Celsius', () => {
66+
it('with Rankine value', () => {
67+
const test1 = tc.rankineToCelsius(234)
68+
expect(test1).toBe(-143)
69+
})
70+
})
71+
describe('Testing Conversion of Rankine to Fahrenheit', () => {
72+
it('with Rankine value', () => {
73+
const test1 = tc.rankineToFahrenheit(98)
74+
expect(test1).toBe(-362)
75+
})
76+
})
77+
describe('Testing Conversion of Rankine to Kelvin', () => {
78+
it('with Rankine value', () => {
79+
const test1 = tc.rankineToKelvin(10)
80+
expect(test1).toBe(6)
81+
})
82+
})
83+
describe('Testing Conversion of Reamur to Celsius', () => {
84+
it('with Reamur value', () => {
85+
const test1 = tc.reaumurToCelsius(100)
86+
expect(test1).toBe(125)
87+
})
88+
})
89+
describe('Testing Conversion of Reamur to Fahrenheit', () => {
90+
it('with Reamur value', () => {
91+
const test1 = tc.reaumurToFahrenheit(100)
92+
expect(test1).toBe(257)
93+
})
94+
})
95+
describe('Testing Conversion of Reamur to Kelvin', () => {
96+
it('with Reamur value', () => {
97+
const test1 = tc.reaumurToKelvin(100)
98+
expect(test1).toBe(398)
99+
})
100+
})
101+
describe('Testing Conversion of Reamur to Rankine', () => {
102+
it('with Reamur value', () => {
103+
const test1 = tc.reaumurToRankine(100)
104+
expect(test1).toBe(717)
105+
})
106+
})

0 commit comments

Comments
 (0)