Programming Fundamentals/Condition Examples JavaScript

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Temperature[edit]

The temperature program main function is listed twice, once using if-then-else and once using switch, followed by the supporting functions.

If Then Else[edit]

// This program asks the user to select Fahrenheit or Celsius conversion
// and input a given temperature. Then the program converts the given 
// temperature and displays the result.
//
// References:
//   https://www.mathsisfun.com/temperature-conversion.html
//   https://en.wikibooks.org/wiki/JavaScript

main();

function main() {
    var choice;
    
    choice = getChoice();
    if (choice == "C" || choice == "c") {
        processCelsius();
    } 
    else if (choice == "F" || choice == "f") {
        processFahrenheit();
    } 
    else {
        output("You must enter C to convert to Celsius or F to convert to Fahrenheit.");
    }
}

Switch[edit]

function main() {
    var choice;
    
    choice = getChoice();
    switch(choice)
    {
        case 'C':
        case 'c':
            processCelsius();
            break;
        case 'F':
        case 'f':
            processFahrenheit();
            break;
        default:
            output("You must enter C to convert to Celsius or F to convert to Fahrenheit.");
    }
}

Supporting Functions[edit]

function getChoice() {
    var choice;
    
    output("Enter C to convert to Celsius or F to convert to Fahrenheit:");
    choice = input();
    
    return choice;
}

function processCelsius() {
    var temperature;
    var result;
    
    temperature = getTemperature("Fahrenheit");
    result = calculateCelsius(temperature);
    displayResult(temperature, "Fahrenheit", result, "Celsius");
}

function processFahrenheit() {
    var temperature;
    var result;
    
    temperature = getTemperature("Celsius");
    result = calculateFahrenheit(temperature);
    displayResult(temperature, "Celsius", result, "Fahrenheit");
}

function getTemperature(scale) {
    var temperature;
    
    output("Enter " + scale + " temperature:");
    temperature = input();
    
    return temperature;
}

function calculateCelsius(fahrenheit) {
    var celsius;
    
    celsius = (fahrenheit - 32) * 5 / 9;
    
    return celsius;
}

function calculateFahrenheit(celsius) {
    var fahrenheit;
    
    fahrenheit = celsius * 9 / 5 + 32;
    
    return fahrenheit;
}

function displayResult(temperature, fromScale, result, toScale) {
    output(temperature.toString() + "° " + fromScale + " is " + result + "° " + toScale);
}

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);
  }
}

Output[edit]

Enter C to convert to Celsius or F to convert to Fahrenheit:
 c
Enter Fahrenheit temperature:
 100
100° Fahrenheit is 37.77777777777778° Celsius

Enter C to convert to Celsius or F to convert to Fahrenheit:
 f
Enter Celsius temperature:
 100
100° Celsius is 212° Fahrenheit

Enter C to convert to Celsius or F to convert to Fahrenheit:
 x
You must enter C to convert to Celsius or F to convert to Fahrenheit.

References[edit]