Fahrenheit to celsius temperature converter
javascript
Instructions:
Create a function called convertToCelsius()
that takes a temperature values in fahrenheit as a parameter and returns the temperature in celsius.
Expected result:
console.log(convertToCelsius(32)); // The temperature is 0 Celsius
Solution:
function convertToCelsius1(temp) {
const valueInCelsius = ((temp - 32) * 5) / 9;
return valueInCelsius;
}
console.log(`The temperature in Celsius: ${convertToCelsius(32)} \xB0C`);
Same solution using arrow function:
const convertToCelsius2 = (temp) => ((temp - 32) * 5) / 9;
console.log(`The temperature in Celsius: ${convertToCelsius(32)} \xB0C`);
Test Values:
console.log(`The temperature in Celsius: ${convertToCelsius1(32)} \xB0C`);
console.log(`The temperature in Celsius: ${convertToCelsius2(32)} \xB0C`);
Output:
The temperature in Celsius: 0 °C
The temperature in Celsius: 0 °C
console.log(`The temperature in Celsius: ${convertToCelsius1(100)} \xB0C`);
console.log(`The temperature in Celsius: ${convertToCelsius2(100)} \xB0C`);
Output:
The temperature in Celsius: 37.77777777777778 °C
The temperature in Celsius: 37.77777777777778 °C
console.log(`The temperature in Celsius: ${convertToCelsius1(50)} \xB0C`);
console.log(`The temperature in Celsius: ${convertToCelsius2(50)} \xB0C`);
Output:
The temperature in Celsius: 10 °C
The temperature in Celsius: 10 °C