Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width-device-width, initial-scale=1.0">
        <title>Tip Calculator</title>
        <link rel="stylesheet" type="text/css" href="styles.css">
    </head>
    <body>
        <div class="main-container">
           <h1>TIP CALCULATOR</h1>

            <div id="calculator">
                <form>
                    <p>How much was your bill?</p>

                        <label for="bill-amount">$</label>
                        <input type="text" id="bill-amount" name="bill-amount" placeholder="Bill amount" required>
                    <p>How was your service?</p>

                    <label for="service"></label>
                    
                    <select name="service-quality" id="service-quality">
                        <option disabled selected value="0">--Choose an Option --</option>
                        <option value="0.3">30&#37; &#45; Outstanding</option>
                        <option value="0.2">20&#37; &#45; Good</option>
                        <option value="0.15">15&#37; &#45; It was OK</option>
                        <option value="0.1">10&#37; &#45; Bad</option>
                        <option value="0.05">5&#37; &#45; Terrible</option>
                    </select>
                </form>

                <p>How many people are splitting the bill?</p>

                <form class="people">
                    <input type="text" id="num-people" name="num-people" placeholder="Number of People" required>
                    <label for="num-people">people</label>
                </form>

                
                <button type="submit" id="calculate-button">CALCULATE!</button>
                
                
            </div>
            <!--Calcualtor end-->
            <div id="totalTip">
                <sup>$</sup><span id="tip">0.00</span>
                <small id="each">each</small>
            </div>
            <!--totalTip ends-->
        
        </div>
        <!--container ends-->

        <script src="app.js" meta="utf-8"></script>
    </body>
    </html>

   
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Roboto:[email protected];700&display=swap');
body {
    background: #BC8F8F;
    font-family: Roboto, sans-serif;
    font-weight: bold;
}

.main-container {
    height: 525px;
    width: 360px;
    margin: 100px auto;
    border: 1px solid black;
    border-radius: 20px;
    background: cornsilk;

    
}

h1 {
    background: darkred;
    text-align: center;
    color: white;
    margin: 0;
    font-size: 18px;
    padding: 10px 10px;
    font-weight: normal;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    
}

p {
    padding-left: 22px;
}

label {
    padding-left: 22px;
}

#bill-amount {
    height: 20px;
    font-size: 15px;
    background-color: #f7f7f7;
    width: 60%;
    padding: 5px 5px 8px 8px;
}

#service-quality {
    height: 35px;
    font-size: 15px;
    background-color: #f7f7f7;
    width: 60%;
    padding: 5px 5px 8px 8px;
}

label[for="service"] {
    padding-left: 35px;
}

.people {
    padding-left: 35px;
}

#num-people {
    background-color: #f7f7f7;
    height: 20px;
    font-size: 15px;
    width: 60%;
    padding: 5px 5px 8px 8px;
    
}

button {
    font-weight: bold;
    display: block;
    margin: 30px auto;
    background: #AD133A;
    border-radius: 5px;
    width: 200px;
    height: 50px;
    font-size: 18px;
    color: white;
}

button:hover {
    background: red;
}

button:active {
    position: relative;
    top: 1px;
}

#totalTip {
    font-size: 35px;
    margin-top: 5px;
    text-align: center;
}

#totalTip:before {
    content: "Tip amount";
    font-size: 20px;
    font-weight: bold;
    display: block;
    text-transform: uppercase;
}

#totalTip sup {
    font-size: 20px;
    top: -18px;
}

#totalTip small {
    font-size: 20px;
    font-weight: bold;
    display: block;
}
              
            
!

JS

              
                // First of all, create a fucntion to calculate the tip

function tipCalculator () {
    let billAmount_div = document.getElementById("bill-amount").value;
    let serviceQuality_label = document.getElementById("service-quality").value;
    let peopleNum_form = document.getElementById("num-people").value;

    //Make sure input is valid
    if (billAmount_div === " " || serviceQuality_label === 0) {
        alert("Please enter valid values");
        return;
    }

    //Check to see if number of people are valid
    if (peopleNum_form === " " || peopleNum_form <= 1) {
        peopleNum_form = 1;
        // This hides the word  "each" if number of people bar is empty or equals 1
        document.getElementById("each").style.display = "none";
    }
    else {
        // This shows it in block form
        document.getElementById("each").style.display = "block";
    }

    //Now to calculate the tip
    let total = (billAmount_div * serviceQuality_label) / peopleNum_form;
    //round to two decimal places
    total = Math.round(total * 100)/100;
    //Allows two digits after decimal point
    total = total.toFixed(2);

    //Now display the tip on browser
    document.getElementById("totalTip").style.display = "block";
    document.getElementById("tip").innerHTML = total;
};

//hide the tip amount when it is loading
document.getElementById("totalTip").style.display = "none";
document.getElementById("each").style.display = "none";

//Now set calculate-button to call function on click
document.getElementById("calculate-button").onclick = function () {
    tipCalculator();
};
              
            
!
999px

Console