How to resolve the algorithm Temperature conversion step by step in the JavaScript programming language
How to resolve the algorithm Temperature conversion step by step in the JavaScript programming language
Table of Contents
Problem Statement
There are quite a number of temperature scales. For this task we will concentrate on four of the perhaps best-known ones: Kelvin, Celsius, Fahrenheit, and Rankine. The Celsius and Kelvin scales have the same magnitude, but different null points. The Fahrenheit and Rankine scales also have the same magnitude, but different null points. The Celsius/Kelvin and Fahrenheit/Rankine scales have a ratio of 5 : 9.
Write code that accepts a value of kelvin, converts it to values of the three other scales, and prints the result.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Temperature conversion step by step in the JavaScript programming language
The provided code is a JavaScript program that converts temperatures from Kelvin to Celsius, Fahrenheit, and Reaumur scales to aid in understanding temperature conversions. Here's a detailed breakdown of the code:
1. Temperature Conversion Functions:
k2c(k): Converts Kelvin (k) to Celsius.k2r(k): Converts Kelvin (k) to Reaumur.k2f(k): Converts Kelvin (k) to Fahrenheit.
2. Number Extensions:
toMaxDecimal(d): This function extends the Number prototype and allows numbers to be rounded to a specified number of decimal places (d).
3. kCnv Function:
- This function takes a Kelvin temperature as input and prints its conversions to Celsius, Reaumur, and Fahrenheit scales with two decimal places of precision.
4. Immediately Invoked Function Expression (IIFE):
- The code wraps itself in an IIFE to create its own private scope.
5. kelvinTranslations Function:
- This function takes a Kelvin temperature as input and returns an array of four-element sub-arrays. Each sub-array contains a scale name (e.g., "K", "C", "F", "R") and the corresponding converted temperature.
6. heatBabel Function:
-
This function performs the actual temperature conversion. It takes three parameters:
n: The temperature value to be converted.strFromScale: The original temperature scale (e.g., "K").strToScale: The desired temperature scale (e.g., "C").
-
heatBabeluses a series of mathematical formulas to convert temperatures between Kelvin, Celsius, Fahrenheit, and Reaumur scales.
7. Usage:
- The code demonstrates the use of
kCnvandkelvinTranslationsfunctions by converting 21 and 295 Kelvin to their corresponding Celsius, Reaumur, and Fahrenheit equivalents. - The IIFE is executed immediately, returning a string that shows the conversions for 21 Kelvin in a specific format.
This code provides a comprehensive solution for converting temperatures between the Kelvin, Celsius, Fahrenheit, and Reaumur scales, making it useful for scientific or educational purposes where temperature conversions are required.
Source code in the javascript programming language
var k2c = k => k - 273.15
var k2r = k => k * 1.8
var k2f = k => k2r(k) - 459.67
Number.prototype.toMaxDecimal = function (d) {
return +this.toFixed(d) + ''
}
function kCnv(k) {
document.write( k,'K° = ', k2c(k).toMaxDecimal(2),'C° = ', k2r(k).toMaxDecimal(2),'R° = ', k2f(k).toMaxDecimal(2),'F°<br>' )
}
kCnv(21)
kCnv(295)
(() => {
'use strict';
let kelvinTranslations = k => ['K', 'C', 'F', 'R']
.map(x => [x, heatBabel(k, 'K', x)]);
// heatBabel :: Num -> ScaleName -> ScaleName -> Num
let heatBabel = (n, strFromScale, strToScale) => {
let ratio = 9 / 5,
cels = 273.15,
fahr = 459.67,
id = x => x,
readK = {
k: id,
c: x => cels + x,
f: x => (fahr + x) * ratio,
r: x => x / ratio
},
writeK = {
k: id,
c: x => x - cels,
f: x => (x * ratio) - fahr,
r: x => ratio * x
};
return writeK[strToScale.charAt(0).toLowerCase()](
readK[strFromScale.charAt(0).toLowerCase()](n)
).toFixed(2);
};
// TEST
return kelvinTranslations(21)
.map(([s, n]) => s + (' ' + n)
.slice(-10))
.join('\n');
})();
You may also check:How to resolve the algorithm File extension is in extensions list step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Emirp primes step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the JavaScript programming language