loops.js
[edit | edit source]/* This program converts a Fahrenheit temperature to Celsius. Input: Fahrenheit temperature Output: Fahrenheit temperature Celsius temperature Example: Enter Fahrenheit temperature: 100 100° Fahrenheit is 37.77777777777778° Celsius ... Enter Fahrenheit temperature or press <Enter> to quit: References: * http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html * https://javascript.info/comments * http://www.mathsisfun.com/temperature-conversion.html * https://nodejs.org/api/process.html#processexitcode * https://javascript.info/try-catch * https://javascript.info/types */ constTEMPERATURE_DIFFERENCE=32; constTEMPERATURE_RATIO=5/9; constABSOLUTE_ZERO=-459.67; main(); /** * Runs main program logic. */ functionmain(){ try{ while(true){ letfahrenheit=getFahrenheit(); if(fahrenheit===""){ break; } letcelsius=calculateCelsius(fahrenheit); displayResults(fahrenheit,celsius); displayTable(fahrenheit); } }catch(error){ output("Unexpected error:"); output(`${error.name}: ${error.message}`); } } /** * Gets Fahrenheit temperature. * * @returns {number} Fahrenheit temperature or empty string */ functiongetFahrenheit(){ while(true){ letfahrenheit=input("Enter Fahrenheit temperature or press <Enter> to quit: "); if(fahrenheit===""){ returnfahrenheit; } if(isNaN(fahrenheit)){ output("Fahrenheit temperature must be a number."); output(`TypeError: '${fahrenheit}' is not a number.`); continue; } fahrenheit=Number(fahrenheit); if(fahrenheit<ABSOLUTE_ZERO){ output("Fahrenheit temperature cannot be below absolute zero."); output(`RangeError: ${fahrenheit} is less than ${ABSOLUTE_ZERO}.`); continue; } returnfahrenheit; } } /** * Converts Fahrenheit temperature to Celsius. * * @param {number} Fahrenheit temperature * @returns {number} Celsius temperature * @throws TypeError if fahrenheit is not numeric. * @throws RangeError if fahrenheit is below absolute zero. */ functioncalculateCelsius(fahrenheit){ if(typeoffahrenheit!="number"){ throw(newTypeError(`'${fahrenheit}' is not a number.`)) } if(fahrenheit<ABSOLUTE_ZERO){ throw(newRangeError(`'${fahrenheit}' is below absolute zero.`)) } letcelsius=(fahrenheit-TEMPERATURE_DIFFERENCE)*TEMPERATURE_RATIO; returncelsius; } /** * Displays Fahrenheit and Celsius temperatures. * * @param {number} Fahrenheit temperature * @param {number} Celsius temperature */ functiondisplayResults(fahrenheit,celsius){ console.assert(typeoffahrenheit=="number"); console.assert(typeofcelsius=="number"); output(`${fahrenheit}° Fahrenheit is ${celsius}° Celsius`); } /** * Displays nearest multiples of 10 Fahrenheit and Celsius temperatures. * * @param {number} Fahrenheit temperature */ functiondisplayTable(fahrenheit){ console.assert(typeoffahrenheit=="number"); output(""); output("F\tC") letstart=Math.floor(fahrenheit/10)*10 for(letfahrenheit=start;fahrenheit<start+11;fahrenheit++){ letcelsius=calculateCelsius(fahrenheit); output(`${fahrenheit.toFixed(1)}\t${celsius.toFixed(1)}`) } output(""); } /** * Generic input function to get input in HTML, Node, or Rhino environments. * * @param {string} text prompt * @returns {string} input */ functioninput(text){ if(typeofwindow==='object'){ returnprompt(text) } elseif(typeofconsole==='object'){ constrls=require('readline-sync'); letvalue=rls.question(text); returnvalue; } else{ output(text); letisr=newjava.io.InputStreamReader(java.lang.System.in); letbr=newjava.io.BufferedReader(isr); letline=br.readLine(); returnline.trim(); } } /** * Generic output function to display output in HTML, Node, or Rhino environments. * * @param {string} text prompt * @returns {string} input */ functionoutput(text){ if(typeofdocument==='object'){ document.write(text); } elseif(typeofconsole==='object'){ console.log(text); } else{ print(text); } }
Try It
[edit | edit source]Copy and paste the code above into one of the following free online development environments or use your own JavaScript compiler / interpreter / IDE.
- Chapman.edu: Online JavaScript Interpreter
- CodeChef
- GDB Online
- Ideone
- JS.do
- paiza.IO
- PythonTutor
- repl.it
