JavaScript | Example of Nested if

JavaScript Example of Nested if: Here, we have an example in which we are demonstrating an example of Nosed if in JavaScript.
Submitted by Pankaj Singh, on October 23, 2018

Example:

In this example, we are reading salary of an employee and finding the discount and net pay based on given salary and discount rate.

Code (JS & HTML):

<!DOCTYPE html>

<HTML>
    <HEAD>
        <SCRIPT>
            function Calculate(){
                var sa=parseInt(document.getElementById("txta").value);
                var disc=0.0;
                var np=0.0;
                if(sa>0){
                    if(sa>5000){
                        if(sa>15000){
                            if(sa>30000){
                                disc=sa*0.4;
                            }
                            else{
                                disc=sa*0.25;
                            }
                        }
                        else{
                           disc=sa*0.15; 
                        }
                    }
                    else{
                        disc=sa*0.05;
                    }
                    
                    document.getElementById("txtb").value=""+disc;
                    var np=sa-disc;
                    document.getElementById("txtc").value=""+np;
                }
                else{
                    alert('Invalid Sale Amount')
                }
                
            }
        </SCRIPT>
    </HEAD>
    <BODY>
        <h2>Nested If : True ladder</h2>
        <hr />
        <table>
            <tr>
                <td>
                    <label>Enter Sale Amount:</label>
                </td>
                <td>
                    <input type="text" name="txta" id="txta" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <input type="button" value="Calculate Discount" onclick="Calculate()" />
                </td>
            </tr>
             <tr>
                <td>
                    <label>Discount</label>
                </td>
                <td>
                    <input type="text" name="txtb" id="txtb" readonly />
                </td>
            </tr>
            <tr>
                <td>
                    <label>Net Pay</label>
                </td>
                <td>
                    <input type="text" name="txtc" id="txtc" readonly />
                </td>
            </tr>
        </table>
    </BODY>
</HTML>

Output

JS | Example of Nested if

JavaScript Examples »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.