Interest Calculator using HTML, CSS and JavaScript
Published: 2023-05-25 05:45:57

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Simple **[Interest Calculator](https://www.codewithrandom.com/2022/11/30/interest-calculator-using-javascript-2/)** which will calculate the simple interest on the borrowed amount from the bank and give the result after applying the percentage of the simple interest. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Simple Interest Calculator Project.

**Step 1**
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Simple Interest Calculator Project.

**Step 2**
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Simple Interest Calculator Project.

**Step 3**
At last we will use JS (JavaScript) which will add a logic to make the Simple Interest Calculator Project responsive from the user end.

I hope you have got an idea about the project.

## HTML Code for Simple Interest Calculator

First we'll start with creating the structure of the Simple Interest Calculator project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.
```
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Simple Interest Calculator</title>
    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="input-wrapper">
        <div class="wrapper">
          <label for="principal">Principal($):</label>
          <input type="number" id="principal" value="1000" />
        </div>
        <div class="wrapper">
          <label for="rate">Rate:</label>
          <input type="number" id="rate" value="5" />
        </div>
      </div>
      <label for="time">Time:</label>
      <div class="time-wrapper">
        <input type="number" id="time" value="1" />
        <select name="duration" id="duration">
          <option value="year">Year</option>
          <option value="month">Month</option>
        </select>
      </div>
      <button id="calculate-btn">Calculate</button>
      <div id="result"></div>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>
```
## CSS Code for Simple Interest Calculator

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Simple Interest Calculator project so that it is properly situated and doesn't get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.
```
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  height: 100vh;
  background: linear-gradient(#01e26e, #72ffb4);
}
.container {
  background-color: #ffffff;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  width: 80vw;
  max-width: 600px;
  min-width: 350px;
  padding: 60px 30px;
  border-radius: 10px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
}
label {
  display: block;
  font-size: 22px;
  margin-bottom: 10px;
  font-weight: 500;
}
input {
  margin-bottom: 20px;
  border: none;
  font-size: 20px;
  border-bottom: 2px solid #585858;
  color: #585858;
  padding: 2px 15px;
}
input:focus {
  outline: none;
  border-bottom: 2.4px solid #01e26e;
}
.input-wrapper {
  display: flex;
  justify-content: space-between;
  gap: 20px;
}
.input-wrapper input {
  width: 100%;
}
.time-wrapper input {
  width: 60%;
}
select {
  width: 35%;
  border: 1px solid #585858;
  font-size: 20px;
  margin-left: 3%;
  padding: 8px 0;
  border-radius: 5px;
}
button {
  display: block;
  background-color: #01e26e;
  border: none;
  color: #ffffff;
  margin: 20px auto 0 auto;
  padding: 15px 40px;
  font-size: 20px;
  border-radius: 5px;
}
#result {
  background-color: #c6ffe2;
  margin-top: 30px;
  color: #585858;
  text-align: center;
  font-size: 18px;
  padding: 20px;
  border-radius: 5px;
}
#result div {
  margin-bottom: 10px;
}
#result span {
  color: #000000;
  font-weight: 500;
}
```
## JavaScript Code for Simple Interest Calculator

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Simple Interest Calculator using HTML, CSS & JavaScript (Source Code).
```
let calculateBtn = document.getElementById("calculate-btn");
let result = document.getElementById("result");
let calculate = () => {
  let p = Number(document.getElementById("principal").value);
  let r = Number(document.getElementById("rate").value);
  let t = Number(document.getElementById("time").value);
  let duration = document.getElementById("duration").value;
  let simpleInterest =
    duration == "year" ? (p * r * t) / 100 : (p * r * t) / 1200;
  let amount = p + simpleInterest;

  result.innerHTML = `<div>Principal Amount: <span>${p.toFixed(2)}</span></div>
  <div>Total Interest: <span>${simpleInterest.toFixed(2)}</span></div>
  <div>Total Amount: <span>${amount.toFixed(2)}</span></div>`;
};
calculateBtn.addEventListener("click", calculate);
window.addEventListener("load", calculate);
```
We have Successfully created our Simple **[Interest Calculator](https://www.codewithrandom.com/2022/11/30/interest-calculator-using-javascript-2/)** using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea - codingartist

Written By – Harsh Sawant

Code By – @harshh9