Temperature Conversion Table  Start: Stop: Step:      Fahrenheit Celsius        // This program displays a temperature conversion chart and demonstrates // JavaScript loops. // // 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; function Temperature(fahrenheit=null, celsius=null) { this.fahrenheit = fahrenheit; this.celsius = celsius; this.fahrenheitToCelsius = function(fahrenheit) { let celsius = (fahrenheit - TEMPERATURE_DIFFERENCE) * TEMPERATURE_RATIO; return celsius; }; this.celsiusToFahrenheit = function(celsius) { let fahrenheit = celsius / TEMPERATURE_RATIO + TEMPERATURE_DIFFERENCE; return fahrenheit; }; } window.addEventListener("load", function () { let elements = document.getElementsByTagName("input"); for (let i = 0; i < elements.length; i++) { elements[i].addEventListener("focus", inputFocus); elements[i].addEventListener("input", inputInput); } document.getElementById("start").focus(); }); function inputFocus() { document.activeElement.select(); document.getElementById("error").innerText = "Enter " + document.activeElement.id + " value. "; } function inputInput() { let value = document.activeElement.value; if (checkInput()) { document.getElementById("error").innerText = ""; let table = createTable(); if (table != null) { displayTable(table); } } } function checkInput() { let value = document.activeElement.value; if (isNaN(value) || value.trim().length == 0) { document.getElementById("error").innerText = document.activeElement.id + " must be a number! "; return false; } let elements = document.getElementsByTagName("input"); for (let i = 0; i < elements.length; i++) { value = elements[i].value; if (isNaN(value) || value.trim().length == 0) { return false; } } return true; } function createTable() { let start = Number(document.getElementById("start").value); let stop = Number(document.getElementById("stop").value); let step = Number(document.getElementById("step").value); if (stop < start) { document.getElementById("error").innerText = "Stop must be greater than or equal to start!" document.getElementById("tbody").innerText = ""; return; } if (step <= 0) { document.getElementById("error").innerText = "Step must be greater than or equal to 0!" document.getElementById("tbody").innerText = ""; return; } let table = []; for (let fahrenheit = start; fahrenheit <= stop; fahrenheit += step) { let element = new Temperature(); element.fahrenheit = fahrenheit; element.celsius = element.fahrenheitToCelsius(fahrenheit); table.push(element); } return table; } function displayTable(table) { let result = ""; for (let row = 0; row < table.length; row++) { result += `${table[row].fahrenheit.toFixed(1)}` result += `${table[row].celsius.toFixed(1)}`; } document.getElementById("tbody").innerHTML = result; }
{'wiki_source': 'wikiversity', 'other_info': "{'content_model': 'wikitext', 'language': 'en', 'title': 'JavaScript Programming/Objects/Example Code', 'type': 'text'}"}