Programming Fundamentals/Functions/JavaScript
< Programming Fundamentals‎ | Functions
functions.js
// This program asks the user for a Fahrenheit temperature,// converts the given temperature to Celsius,// and displays the results.//// References:// https://www.mathsisfun.com/temperature-conversion.html​// https://en.wikibooks.org/wiki/JavaScript​main​();​function main() { var fahrenheit = getFahrenheit(); var celisus = calculateCelsius​(​fahrenheit​); displayResult​(​fahrenheit​, celisus);}function getFahrenheit() { var fahrenheit = input("Enter Fahrenheit temperature:"); return fahrenheit;}function calculateCelsius​(​fahrenheit​) { var celisus = (fahrenheit - 32) * 5 / 9; return celisus;}function displayResult​(​fahrenheit​, celisus) { output(fahrenheit + "° Fahrenheit is " + celisus + "° Celsius");}function input(text) { if (typeof window === 'object') { return prompt(text) } else if (typeof console === 'object') { const rls = require​(​'readline-sync'​); var value = rls.question(text); return value; } else { output(text); var isr = new java​.​io​.​InputStreamReader​(​java​.​lang​.​System​.​in​); var br = new java​.​io​.​BufferedReader​(​isr​); var line = br.readLine(); return line.trim(); }}function output(text) { if (typeof document === 'object') { document.write(text); } else if (typeof console === 'object') { console.log(text); } else { print(text); }}
Try It
Copy and paste the code above into one of the following free online development environments or use your own JavaScript compiler / interpreter / IDE.
See Also
Wikibooks: JavaScript
← JavaProgramming Fundamentals/FunctionsLua →
Last edited on 19 April 2020, at 14:45
Wikiversity
Content is available under CC BY-SA 3.0 unless otherwise noted.
Privacy policy
Terms of Use
Desktop
 Home Random Log in  Settings  Donate  About Wikiversity  Disclaimers
WatchEdit