JavaScript Programming/Variables and Expressions/Example Code

example.html

edit
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="Cache-Control" content="no-cache">
  <title>Example</title>
  <script defer src="example.js"></script>
</head>

<body>
  <noscript>Enable JavaScript to see web page content.</noscript>

  <p>A temperature of <span id="fahrenheit"></span> degrees Fahrenheit is equivalent to
     a temperature of <span id="celsius"></span> degrees Celsius.</p>
</body>

</html>

example.js

edit
// This program converts a Fahrenheit temperature to Celsius.
//
// References:
//   https://www.mathsisfun.com/temperature-conversion.html
//   https://en.wikibooks.org/wiki/JavaScript
//
// For more information on on the template literal used in the console.log statements:
//   https://en.wikipedia.org/wiki/String_interpolation#JavaScript


"use strict";

debugger;

const TEMPERATURE_DIFFERENCE = 32;
const TEMPERATURE_RATIO = 5 / 9;

let fahrenheit = window.prompt("Enter Fahrenheit temperature:");
console.log(`Input: ${typeof fahrenheit}`);

fahrenheit = Number(fahrenheit);
console.log(`Number: ${typeof fahrenheit}`);

let celsius = (fahrenheit - TEMPERATURE_DIFFERENCE) * TEMPERATURE_RATIO;
console.log(`Calculation: ${typeof celsius}`);

celsius = celsius.toFixed(1);
console.log(`Fixed: ${typeof celsius}`);

document.getElementById("fahrenheit").innerText = fahrenheit;
document.getElementById("celsius").innerText = celsius;