Applied Programming/Conditions/JavaScript

/*
This program converts a Fahrenheit temperature to Celsius.

Input:
    Fahrenheit temperature

Output:
    Fahrenheit temperature
    Celsius temperature

Example:
    Enter Fahrenheit temperature:
    68
    68° Fahrenheit is 20° Celsius

References
    * http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
    * http://www.mathsisfun.com/temperature-conversion.html
    * http://tramaine.me/blog/use-typeof-to-check-javascript-data-types-at-runtime
    * https://javascript.info/try-catch
    * https://javascript.info/types
*/


function get_fahrenheit() {
    /* Gets Fahrenheit temperature.

    Args:
        None

    Returns:
        number: Fahrenheit temperature

    */
	var fahrenheit = input("Enter Fahrenheit temperature:");
	if (isNaN(Number(fahrenheit))) {
    try {
        throw new Error('The entered fahrenheit tempuature | ' + fahrenheit + ' | is not in the correct format of NUMBER. An example of a valid entry may be | 32 | or | 45.4 |!')
    } catch(e) {
        alert(e);
        console.log(e);
    }
	} else if (Number(fahrenheit) < -459.67) {
    try {
        throw new Error('The entered fahrenheit tempuature | ' + fahrenheit + ' | is below absolute zero | -459.67 |!')
    } catch(e) {
        alert(e);
        console.log(e);
    }	  
	} else {
	  fahrenheit = Number(fahrenheit);
	  return(fahrenheit);
	}
}


function fahrenheit_to_celsius(fahrenheit) {
    /* Converts Fahrenheit temperature to Celsius.

    Args:
        fahrenheit (number): Fahrenheit temperature to be converted.

    Returns:
        number: Celsius temperature

    */
	if (isNaN(Number(fahrenheit))) {
    try {
        throw new Error('The passed argument fahrenheit | ' + fahrenheit + ' | is not in the correct format of NUMBER. An example of a valid entry may be | 32 | or | 45.4 |!')
    } catch(e) {
        alert(e);
        console.log(e);
    }
	} else if (Number(fahrenheit) < -459.67) {
    try {
        throw new Error('The passed argument fahrenheit | ' + fahrenheit + ' | is below absolute zero | -459.67 |!')
    } catch(e) {
        alert(e);
        console.log(e);
    }	  
	} else {
  	const TEMPERATURE_DIFFERENCE = 32;
  	const TEMPERATURE_RATIO = 5 / 9;
  	celsius = (fahrenheit - TEMPERATURE_DIFFERENCE) * TEMPERATURE_RATIO;
  	return celsius;
  }
}


function display_results(fahrenheit, celsius) {
    /* Displays Fahrenheit and Celsius temperatures.

    Args:
        fahrenheit (number): Fahrenheit temperature
        celsius (number): Celsius temperature

    Returns:
        None

    */

	output(fahrenheit + "° Fahrenheit is " + celsius + "° Celsius");
}


function input(text) {
	/* Allows user input

    Args:
        text

    Returns:
        text

    */
	if (typeof console === 'object') {
		return prompt(text)
	}
	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 console === 'object') {
      console.log(text);
    } 
    else if (typeof document === 'object') {
      document.write(text);
    } 
    else {
      print(text);
    }
  }
  

function main() {
	/* Runs the main program logic. */


	fahrenheit = get_fahrenheit();
	celsius = fahrenheit_to_celsius(fahrenheit);
	display_results(fahrenheit, celsius);
}


main();

Try ItEdit

Copy and paste the code above into one of the following free online development environments or use your own JavaScript compiler / interpreter / IDE.

See AlsoEdit