कंप्यूटर प्रोग्रामिंग/कंडीशन/जावास्क्रिप्ट

conditions.js सम्पादन

// 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.

main();

function main()
{
    ifElse();
    switchCase();
}

function ifElse()
{
    var choice;
    
    choice = input("Enter F to convert to Fahrenheit or C to convert to Celsius:");
    if (choice == "C" || choice == "c") 
    {
        toCelsius();
    } 
    else if (choice == "F" || choice == "f") 
    {
        toFahrenheit();
    } 
    else 
    {
        output("You must enter C to convert to Celsius or F to convert to Fahrenheit!");
    }
}

function switchCase()
{
    var choice;
    
    choice = input("Enter F to convert to Fahrenheit or C to convert to Celsius:");
    switch(choice)
    {
        case 'C':
        case 'c':
            toCelsius();
            break;
        case 'F':
        case 'f':
            toFahrenheit();
            break;
        default:
            output("You must enter C to convert to Celsius or F to convert to Fahrenheit!");
    }
}

function toCelsius()
{
    var f;
    var c;
    
    f = input("Enter Fahrenheit temperature:");
    c = (f - 32) * 5 / 9;
    output(f + "° Fahrenheit is " + c + "° Celsius");
}

function toFahrenheit()
{
    var c;
    var f;
    
    c = input("Enter Celsius temperature:");
    f = c * 9 / 5 + 32;
    output(c + "° Celsius is " + f + "° Fahrenheit");
}

function input(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);
    }
}

कोशिश करो सम्पादन

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें सम्पादन