Profit & Loss Calculator in JavaScript

Profit & Loss Calculator in JavaScript
Code Snippet:Profit and Loss (Accounting) - JavaScript Visual Calculator
Author: Tey
Published: March 1, 2024
Last Updated: March 4, 2024
Downloads: 302
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code powers a Profit and loss Visual Calculator. It takes your sales and expenses amounts to calculate your profit or loss instantly. Just enter the sales and expenses figures, hit ‘Calculate,’ and get immediate feedback on your financial situation.

You can use this code on websites needing quick profit and loss calculations. It’s handy for small businesses or entrepreneurs to swiftly determine their financial standing.

How to Create a Profit & Loss Calculator in JavaScript

1. First of all, load the Bootstrap CSS by adding the following CDN link into the head tag of your HTML document.

<link rel='stylesheet' href='https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css'>

2. Copy the following HTML code into your webpage where you want to display the calculator.

<div class="container-fluid">
  <div class="row">
    <div class="col-md"></div>
    <div class="col col-md-6">

      <div class="spacer-0">
        <h1 class="text-success">Profit & Loss Visual Calculator</h1>

        <div class="spacer-2">
          <div class="form-group">
            <label>Amount of sales ($)</label>
            <input id="input-sales" type="number" 
                   class="form-control" required />
          </div>
          <div class="form-group">
            <label>Amount of expenses ($)</label>
            <input id="input-losses" type="number" 
                   class="form-control" required />
          </div>

          <div id="profit-n-loss-result" 
               class="alert alert-primary d-none"></div>

          <button id="calculator-profit-n-loss"
                  class="btn btn-warning">
            Calculate</button>

          <!-- Not functional in codepen --->
          <!--
          <button id="refresh-calculator"
                  class="btn btn-danger text-white float-right">
            Refresh</button>
          -->
        </div>
      </div>
    </div>
    <div class="col-md greyed"></div>
  </div>
</div>

3. The CSS code is there to style the calculator’s elements. Ensure the following CSS is included in your HTML or in a linked stylesheet.

.greyed {
    background-color: #f8f9fa;
}

.spacer-0 {
    margin: 2rem 0;
}

.spacer-2 {
    margin: 3.2rem 0;
}

4. Finally, copy the following JavaScript code into a <script> tag within your HTML file or link an external JavaScript file.

// Profit & Loss Calculator
var calculator = document
    .getElementById('calculator-profit-n-loss')
    .addEventListener('click', profit_N_loss);

var refresh_calculator = document
    .getElementById('refresh-calculator')
    .addEventListener('click', refresh_calc);

function refresh_calc() {
    location.reload();
}

function profit_N_loss(income_raw, loss_raw) {
    var income_raw = document.getElementById('input-sales').value;
    var loss_raw = document.getElementById('input-losses').value;

    income = parseInt(income_raw);
    loss = parseInt(loss_raw);
    var value = income - loss;

    var result_div = document.getElementById('profit-n-loss-result');
    result_div.classList.remove('d-none');
    if (value > 0) {
        result_div.innerHTML = 'Your profit is $ ' + value;
    } else {
        result_div.innerHTML = 'Your loss is $ ' + value;
    }
}

That’s all! hopefully, you have successfully created a Profit and loss Calculator on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X