Let's practice an Easy coding interview problem to convert Temperature from Celsius to Kelvin.
Problem statement:
Given a temperature in Celsius, convert the temperature from Celsius to Kelvin. As an output, print both the values of the temperature in Celsius and Kelvin. The relation between the temperature in Celsius and Kelvin is defined using the following formulae:
Temperature (Kelvin) = Temperature (Celsius) + 273
Solution:
Based on the above problem statement, the Javascript code for temperature conversion is given below:
let celsius = 29;
let kelvin = celsius + 273
console.log("Temperature")
console.log("Celsius:", celsius)
console.log("Kelvin:", kelvin)
Here, we have initialized a variable called "celsius" which is used to store the input temperature in celsius..123mylist
In the next step, we have initialized another variable called "kelvin". This variable will be used to store the temperature values in kelvin. The value of the "kelvin" variable is assigned using the formula "K = C + 273".
In the subsequent steps, the values of the temperature are printed on the console using console log statement of Javascript. As per the problem statement, we have printed the values of the temperature in both Celsius and Kelvin.