mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
chore: merge Added Temperature Conversions to Javascript (#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>
This commit is contained in:
103
Conversions/TemperatureConversion.js
Normal file
103
Conversions/TemperatureConversion.js
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
// This files has functions to convert different temperature units
|
||||||
|
// Functions take temperature value as a arguement and returns corresponding converted value
|
||||||
|
|
||||||
|
const celsiusToFahrenheit = (celsius) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
|
||||||
|
return Math.round(((celsius) * 9 / 5) + 32)
|
||||||
|
}
|
||||||
|
|
||||||
|
const celsiusToKelvin = (celsius) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
|
||||||
|
return Math.round((celsius) + 273.15)
|
||||||
|
}
|
||||||
|
|
||||||
|
const celsiusToRankine = (celsius) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
|
||||||
|
return Math.round(((celsius) * 9 / 5) + 491.67)
|
||||||
|
}
|
||||||
|
|
||||||
|
const fahrenheitToCelsius = (fahrenheit) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
|
||||||
|
return Math.round(((fahrenheit) - 32) * 5 / 9)
|
||||||
|
}
|
||||||
|
|
||||||
|
const fahrenheitToKelvin = (fahrenheit) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
|
||||||
|
return Math.round((((fahrenheit) - 32) * 5 / 9) + 273.15)
|
||||||
|
}
|
||||||
|
|
||||||
|
const fahrenheitToRankine = (fahrenheit) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
|
||||||
|
return Math.round((fahrenheit) + 459.67)
|
||||||
|
}
|
||||||
|
|
||||||
|
const kelvinToCelsius = (kelvin) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
|
||||||
|
return Math.round((kelvin) - 273.15)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const kelvinToFahrenheit = (kelvin) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
|
||||||
|
return Math.round((((kelvin) - 273.15) * 9 / 5) + 32)
|
||||||
|
}
|
||||||
|
|
||||||
|
const kelvinToRankine = (kelvin) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
|
||||||
|
return Math.round(( (kelvin) * 9 / 5))
|
||||||
|
}
|
||||||
|
|
||||||
|
const rankineToCelsius = (rankine) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
|
||||||
|
return Math.round(((rankine) - 491.67) * 5 / 9)
|
||||||
|
}
|
||||||
|
|
||||||
|
const rankineToFahrenheit = (rankine) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
|
||||||
|
return Math.round((rankine) - 459.67)
|
||||||
|
}
|
||||||
|
|
||||||
|
const rankineToKelvin = (rankine) => {
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
|
||||||
|
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
|
||||||
|
return Math.round(((rankine) * 5 / 9))
|
||||||
|
}
|
||||||
|
|
||||||
|
const reaumurToKelvin = (reaumur) => {
|
||||||
|
// Reference:- http://www.csgnetwork.com/temp2conv.html
|
||||||
|
return Math.round(((reaumur) * 1.25 + 273.15))
|
||||||
|
}
|
||||||
|
|
||||||
|
const reaumurToFahrenheit = (reaumur) => {
|
||||||
|
// Reference:- http://www.csgnetwork.com/temp2conv.html
|
||||||
|
return Math.round(((reaumur) * 2.25 + 32))
|
||||||
|
}
|
||||||
|
|
||||||
|
const reaumurToCelsius = (reaumur) => {
|
||||||
|
// Reference:- http://www.csgnetwork.com/temp2conv.html
|
||||||
|
return Math.round(((reaumur) * 1.25))
|
||||||
|
}
|
||||||
|
|
||||||
|
const reaumurToRankine = (reaumur) => {
|
||||||
|
// Reference:- http://www.csgnetwork.com/temp2conv.html
|
||||||
|
return Math.round(((reaumur) * 2.25 + 32 + 459.67))
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
celsiusToFahrenheit, celsiusToKelvin, celsiusToRankine,
|
||||||
|
fahrenheitToCelsius, fahrenheitToKelvin, fahrenheitToRankine,
|
||||||
|
kelvinToCelsius, kelvinToFahrenheit, kelvinToRankine,
|
||||||
|
rankineToCelsius, rankineToFahrenheit, rankineToKelvin,
|
||||||
|
reaumurToCelsius, reaumurToFahrenheit, reaumurToKelvin, reaumurToRankine
|
||||||
|
}
|
106
Conversions/test/TemperatureConversion.test.js
Normal file
106
Conversions/test/TemperatureConversion.test.js
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
import * as tc from '../TemperatureConversion.js'
|
||||||
|
|
||||||
|
describe('Testing Conversion of Celsius to fahrenheit', () => {
|
||||||
|
it('with celsius value', () => {
|
||||||
|
const test1 = tc.celsiusToFahrenheit(10)
|
||||||
|
expect(test1).toBe(50)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Testing Conversion of Celsius to kelvin', () => {
|
||||||
|
it('with celsius value', () => {
|
||||||
|
const test1 = tc.celsiusToKelvin(15)
|
||||||
|
expect(test1).toBe(288)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Testing Conversion of Celsius to Rankine', () => {
|
||||||
|
it('with celsius value', () => {
|
||||||
|
const test1 = tc.celsiusToRankine(28)
|
||||||
|
expect(test1).toBe(542)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Testing Conversion of Fahrenheit to Celsius', () => {
|
||||||
|
it('with Fahrenheit value', () => {
|
||||||
|
const test1 = tc.fahrenheitToCelsius(134)
|
||||||
|
expect(test1).toBe(57)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Testing Conversion of Fahrenheit to Kelvin', () => {
|
||||||
|
it('with Fahrenheit value', () => {
|
||||||
|
const test1 = tc.fahrenheitToKelvin(125)
|
||||||
|
expect(test1).toBe(325)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Testing Conversion of Fahrenheit to Rankine', () => {
|
||||||
|
it('with Fahrenheit value', () => {
|
||||||
|
const test1 = tc.fahrenheitToRankine(10)
|
||||||
|
expect(test1).toBe(470)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Testing Conversion of Kelvin to Celsius', () => {
|
||||||
|
it('with Kelvin value', () => {
|
||||||
|
const test1 = tc.kelvinToCelsius(100)
|
||||||
|
expect(test1).toBe(-173)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Testing Conversion of Kelvin to Fahrenheit', () => {
|
||||||
|
it('with Kelvin value', () => {
|
||||||
|
const test1 = tc.kelvinToFahrenheit(20)
|
||||||
|
expect(test1).toBe(-424)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Testing Conversion of Kelvin to Rankine', () => {
|
||||||
|
it('with kelvin value', () => {
|
||||||
|
const test1 = tc.kelvinToRankine(69)
|
||||||
|
expect(test1).toBe(124)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('Testing Conversion of Rankine to Celsius', () => {
|
||||||
|
it('with Rankine value', () => {
|
||||||
|
const test1 = tc.rankineToCelsius(234)
|
||||||
|
expect(test1).toBe(-143)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('Testing Conversion of Rankine to Fahrenheit', () => {
|
||||||
|
it('with Rankine value', () => {
|
||||||
|
const test1 = tc.rankineToFahrenheit(98)
|
||||||
|
expect(test1).toBe(-362)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('Testing Conversion of Rankine to Kelvin', () => {
|
||||||
|
it('with Rankine value', () => {
|
||||||
|
const test1 = tc.rankineToKelvin(10)
|
||||||
|
expect(test1).toBe(6)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('Testing Conversion of Reamur to Celsius', () => {
|
||||||
|
it('with Reamur value', () => {
|
||||||
|
const test1 = tc.reaumurToCelsius(100)
|
||||||
|
expect(test1).toBe(125)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('Testing Conversion of Reamur to Fahrenheit', () => {
|
||||||
|
it('with Reamur value', () => {
|
||||||
|
const test1 = tc.reaumurToFahrenheit(100)
|
||||||
|
expect(test1).toBe(257)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('Testing Conversion of Reamur to Kelvin', () => {
|
||||||
|
it('with Reamur value', () => {
|
||||||
|
const test1 = tc.reaumurToKelvin(100)
|
||||||
|
expect(test1).toBe(398)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('Testing Conversion of Reamur to Rankine', () => {
|
||||||
|
it('with Reamur value', () => {
|
||||||
|
const test1 = tc.reaumurToRankine(100)
|
||||||
|
expect(test1).toBe(717)
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user