How to Implement SwitchCase in Javascript


Script:

<script language="javascript" type="text/javascript">
// <!CDATA[

        function Button_onclick(operator) {
            var Operand1 = parseInt(document.getElementById("Text1").value);
            var Operand2 = parseInt(document.getElementById("Text2").value);
            switch (operator) {
                case "+":
                    document.getElementById("Result").innerHTML = Operand1 + Operand2;
                    break;
                case "-":
                    document.getElementById("Result").innerHTML = Operand1 – Operand2;
                    break;
                case "*":
                    document.getElementById("Result").innerHTML = Operand1 * Operand2;
                    break;
                case "/":
                    document.getElementById("Result").innerHTML = Operand1 / Operand2;
                    break;
                case "%":
                    document.getElementById("Result").innerHTML = Operand1 % Operand2;
                    break;
            }
        }

// ]]>
    </script>

TotalCode:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Calculator.aspx.cs" Inherits="Calculator" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>

    <script language="javascript" type="text/javascript">
// <!CDATA[

        function Button_onclick(operator) {
            var Operand1 = parseInt(document.getElementById("Text1").value);
            var Operand2 = parseInt(document.getElementById("Text2").value);
            switch (operator) {
                case "+":
                    document.getElementById("Result").innerHTML = Operand1 + Operand2;
                    break;
                case "-":
                    document.getElementById("Result").innerHTML = Operand1 – Operand2;
                    break;
                case "*":
                    document.getElementById("Result").innerHTML = Operand1 * Operand2;
                    break;
                case "/":
                    document.getElementById("Result").innerHTML = Operand1 / Operand2;
                    break;
                case "%":
                    document.getElementById("Result").innerHTML = Operand1 % Operand2;
                    break;
            }
        }

// ]]>
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    Operand1
                </td>
                <td>
                    <input id="Text1" type="text" />
                </td>
            </tr>
            <tr>
                <td>
                    Operand2
                </td>
                <td>
                    <input id="Text2" type="text" />
                </td>
            </tr>
            <tr>
                <td>
                    Result:
                </td>
                <td id="Result">
                </td>
            </tr>
            <tr>
                <td>
                    Operator
                </td>
                <td>
                    <input id="Button1" type="button" value="+" onclick="return Button_onclick('+');" />
                    <input id="Button2" type="button" value="-" onclick="return Button_onclick('-');" />
                    <input id="Button3" type="button" value="*" onclick="return Button_onclick('*');" />
                    <input id="Button4" type="button" value="/" onclick="return Button_onclick('/');" />
                    <input id="Button5" type="button" value="%" onclick="return Button_onclick('%');" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Output:

Math Functions

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s