1 minute read

You are given a non-negative floating point number rounded to two decimal places celsius, that denotes the temperature in Celsius.

You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit].

Return the array ans. Answers within 10-5 of the actual answer will be accepted.

Note that:

Kelvin = Celsius + 273.15 Fahrenheit = Celsius * 1.80 + 32.00

Example 1:

Input: celsius = 36.50 Output: [309.65000,97.70000] Explanation: Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70. Example 2:

Input: celsius = 122.11 Output: [395.26000,251.79800] Explanation: Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.

/**
 * @param {number} celsius
 * @return {number[]}
 */
var convertTemperature = function (celsius) {
  const kel = celsius + 273.15;
  const Fa = celsius * 1.8 + 32.0;
  const ans = [kel, Fa];
  return ans;
};

var convertTemperature = function(celsius) { ... }: This line declares a variable convertTemperature and assigns it an anonymous function. The function takes a single parameter celsius, which is expected to be a number representing a temperature value in Celsius.

const kel = celsius + 273.15;: This line calculates the temperature in Kelvin by adding 273.15 to the input celsius temperature.

const Fa = celsius _ 1.80 + 32.00;: This line calculates the temperature in Fahrenheit by converting the input celsius temperature to Fahrenheit using the formula: (Celsius _ 1.80) + 32.

const ans = [kel, Fa];: The converted temperature values are stored in an array called ans, with the first element being the temperature in Kelvin and the second element being the temperature in Fahrenheit.

return ans;: The function returns the ans array containing the converted temperatures.

Leave a comment