Javascript-program för att konvertera Celsius till Fahrenheit

Innehållsförteckning

I det här exemplet lär du dig att konvertera Celsius-värde till Fahrenheit i JavaScript.

För att förstå detta exempel bör du ha kunskap om följande JavaScript-programmeringsämnen:

  • JavaScript-variabler och konstanter
  • JavaScript-operatörer

Du kan konvertera celsiusvärdet till fahrenheit med formeln:

 fahrenheit = celsius * 1.8 + 32

Exempel: Celsius till Fahrenheit

 // program to convert celsius to fahrenheit // ask the celsius value to the user const celsius = prompt("Enter a celsius value: "); // calculate fahrenheit const fahrenheit = (celsius * 1.8) + 32 // display the result console.log(`$(celsius) degree celsius is equal to $(fahrenheit) degree fahrenheit.`);

Produktion

 Ange ett celsiusvärde: 55 55 grader celsius är lika med 131 grader fahrenheit.

I programmet ovan anger användaren celsiusvärdet och lagras i celsius-variabeln. Sedan används fahrenheit-formeln för att konvertera celsiusvärde till fahrenheit.

Du kan konvertera fahrenheitvärde till celsius med formeln:

 celsius = (fahrenheit - 32) / 1.8

Intressanta artiklar...