Convert temperature

Write a function that converts Celsius to Fahrenheit & Kelvin.


Temperature conversion is the process of changing the value of temperature from one unit to another.

Temperature can be measured on scales such as Celsius, Fahrenheit and Kelvin.

Celsius scale → freezing point and the boiling point of water is 0 °C and 100 °C respectively.

Fahrenheit scale → freezing point and the boiling point of water is 32 °F and 212 °F respectively.

Kelvin scale → freezing point and the boiling point of water is 273.15 K and 373.15 K respectively.


Example ...

Enter a number to represent temperature in °C ...

0 °C temperature in Celcius

converts to ...

32 °F temperature in Fahrenheit

273.15 K temperature in Kelvin

Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Destructuring

The destructuring syntax is a JavaScript syntax that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

const arr2 = ["Hello", "World"]; → array

const [a, b] = arr2;

console.log(a); returns ↴

"Hello" first element in array

console.log(b); returns ↴

"World" second element in array

Array destructuring assigns the first and second elements of the array directly to a and b, respectively. This eliminates the need for additional indexing, making the code cleaner and more intuitive.


Temperature conversion formulas

Celsius to Fahrenheit ↴

Fahrenheit = (Celcius × 9 ⁄ 5 ) + 32

Celsius to Kelvin ↴

Kelvin = Celcius + 273.15


Initialize a variable to hold the number representing the temperature in Celsius.

const tempC = 100; → user input


Define a function convertTempC to convert temperature.

function convertTempC(degC) {}

The function takes a number as input degC and using the conversion formulas, converts it to Fahrenheit and Kelvin.

Convert degC to Fahrenheit using formula ...

const degF == (degC * 9) / 5 + 32 degF

Convert degC to Kelvin using formula ...

const degK = degC + 273.15 degC

Use destructuring to assign the returned values directly to degF and degK variables.

Call the function and destructure the results.

const [degF, degK ] = convertTempC(tempC)

degF returns → 212 Fahrenheit value

degK returns → 373.15 Kelvin value

The results from the function may then be manipulated, for example, to display results to 2 decimal places.


Call the function with ↴

convertTempC(tempC);


Convert Celsius to Fahrenheit & Kelvin.

const tempC = 100;

function convertTempC(degC) {

const degF = (degC * 9) / 5 + 32;

const degK = degC + 273.15;

return [degF, degK];

}

call function

const [degF, degK ] = convertTempC(tempC);

degF returns → 212

degK returns → 373.15


Alternative - create an array to hold the converted temperatures ↴

Convert Celsius to Fahrenheit & Kelvin.

const tempC = 100;

function convertTempC(celsius) {

const arr = [];

arr[0] = (celsius * 9) / 5 + 32;

arr[1] = celsius + 273.15;

return arr;

}

call function

convertTempC(tempC); returns ↴

[212, 373.15] → array contains both values

convertTempC(tempC)[0]; returns ↴

212 → first index of array [0]

convertTempC(tempC)[1]; returns ↴

373.15 → second index of array [1]

Convert temperature

Enter temperature in Celsius