Home » JavaScript

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.