Home » JavaScript

JavaScript | Example of if else if

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

Example:

In this example, we are reading salary of an employee and finding the discount 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){
                        disc=sa*0.05;
                    } 
                    else if(sa<=15000){
                        disc=sa*0.15;
                    }
                    else if(sa<=30000){
                        disc=sa*0.25;
                    }
                    else{
                        disc=sa*.4;
                    }
                      
                    document.getElementById("txtb").value=""+disc;
                    var np=sa-disc;
                    document.getElementById("txtc").value=""+np;
                }
                else{
                    alert('Invalid Sale Amount')
                }
                
            }
        </SCRIPT>
    </HEAD>
    <BODY>
        <h2>Nested Id : False 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

if else if example | js





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.