Convert Fahrenheit to Celsius in JavaScript
C = (F - 32) * 5/9. 32°F should be 0. If you get 17.7 you forgot the minus 32.
BeginnerBasic ProgramsExample 8 of 10
convert-fahrenheit-to-celsius.js
1// Formula: Celsius = (Fahrenheit - 32) * 5/92let fahrenheit = 98.6;3let celsius = (fahrenheit - 32) * 5 / 9;45console.log(`${fahrenheit}°F = ${celsius.toFixed(2)}°C`);67// Function version8function fahrenheitToCelsius(f) {9 return (f - 32) * 5 / 9;10}1112let temp1 = fahrenheitToCelsius(32); // Freezing point13let temp2 = fahrenheitToCelsius(212); // Boiling point14let temp3 = fahrenheitToCelsius(98.6); // Body temperature1516console.log(`32°F = ${temp1}°C`);17console.log(`212°F = ${temp2}°C`);18console.log(`98.6°F = ${temp3.toFixed(2)}°C`);
Output
98.6°F = 37.00°C 32°F = 0°C 212°F = 100°C 98.6°F = 37.00°C
What's going on
(f - 32) * 5/9. Parentheses matter in your head even when precedence saves you.
32 → 0, 212 → 100. Those are the sanity checks.