GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Main Page
  • Browse
  • Recent changes
  • Guided tours
  • Random
  • Help
  • Portal
  • Colloquium
  • News
  • Projects
  • Sandbox
  • Search
  • Donate
  • Create account

This view

  • Original page
  • New address

GeezerVison is showing en.wikiversity.org — JavaScript Programming/Functions/Example Code - Wikiversity

< JavaScript Programming | Functions

example.html

<!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><output id="fahrenheit"></output>° Fahrenheit is <output id="celsius"></output>° Celsius</p>
</body>

</html>

example.js

// This program converts a Fahrenheit temperature to Celsius.
//
// References:
//   https://www.mathsisfun.com/temperature-conversion.html
//   https://en.wikibooks.org/wiki/JavaScript

"use strict";

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

main();

function main() {
  let fahrenheit = getFahrenheit();
  let celsius = fahrenheitToCelsius(fahrenheit);
  displayResults(fahrenheit, celsius);
}

function getFahrenheit() {
  let fahrenheit = window.prompt("Enter Fahrenheit temperature:");
  fahrenheit = Number(fahrenheit);
  return fahrenheit;
}

function fahrenheitToCelsius(fahrenheit) {
  let celsius = (fahrenheit - TEMPERATURE_DIFFERENCE) * TEMPERATURE_RATIO;
  return celsius;
}

function displayResults(fahrenheit, celsius) {
  celsius = celsius.toFixed(1);
  document.getElementById("fahrenheit").innerText = fahrenheit;
  document.getElementById("celsius").innerText = celsius;
}
Retrieved from "https://en.wikiversity.org/w/index.php?title=JavaScript_Programming/Functions/Example_Code&oldid=2141593"
Category:
  • JavaScript Programming
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.