Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add awesome loan calculator #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions loan-calc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Handy Loan Calculator

**here is the link** [Calc](https://ruhanrk.github.io/loan-calc/).
19 changes: 19 additions & 0 deletions loan-calc/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@import url('https://fonts.googleapis.com/css?family=Merienda');
@import url('https://fonts.googleapis.com/css?family=Satisfy');

#loading,
#results{
display: none;
}

.card2{
background-color: transparent;
}

.heading{
font-family: 'Merienda', cursive;
}
.submits{
font-family: 'Satisfy', cursive;
font-size: 25px;
}
Binary file added loan-calc/img/favicon.ico
Binary file not shown.
Binary file added loan-calc/img/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 91 additions & 0 deletions loan-calc/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Favicon -->
<link href="img/favicon.ico" rel="icon" type="image/x-icon" />
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<!-- Own css -->
<link rel="stylesheet" href="css/style.css">
<title>Loan Calculator</title>
</head>
<body class="bg-secondary">
<div class="container">
<div class="row">
<div class="col-md-6 mx-auto">
<div class="card card1 card-body text-center mt-5">
<h3 class="heading pb-2 pt-1 mb-3 shadow bg-white rounded border border-dark">Loan Calculator</h3>
<form id="loan-form">
<div class="form-group">
<div class="input-group">
<span class="input-group-text">₹</span>
<input type="number" class="form-control" id="amount" placeholder="Loan Amount">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-text">%</span>
<input type="number" class="form-control" id="interest" placeholder="Interest">
</div>
</div>
<div class="form-group">
<input type="number" class="form-control" id="years" placeholder="Years to pay">
</div>
<div class="form-group">
<input type="submit" value="Calculate" class="btn btn-info btn-block submits">
</div>
</form>
</div>
</div>
</div>
</div>

<div class="container result">
<div class="row">
<div class="col-md-6 mx-auto">
<div class="card card2 card-body text-center mt-5">

<!-- LOADER -->
<div id="loading">
<img src="img/loading.gif" alt="">
</div>
<!-- RESULTS -->
<div id="results" class="pt-1 ">
<h5 class="badge badge-dark p-3 mb-3">Results</h5>
<div class="form-group">
<div class="input-group">
<span class="input-group-text">Monthly Payment</span>
<input type="number" class="form-control" id="monthly-payment" disabled>
</div>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-text">Total Payment</span>
<input type="number" class="form-control" id="total-payment" disabled>
</div>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-text">Total Interest</span>
<input type="number" class="form-control" id="total-interest" disabled>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" ></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<!-- own JS -->
<script src="js/app.js"></script>
</body>
</html>
75 changes: 75 additions & 0 deletions loan-calc/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// all UI value

const amount = document.getElementById('amount');
const interest = document.getElementById('interest');
const year = document.getElementById('years');

const monthlyPayment = document.getElementById('monthly-payment');
const totalPayment = document.getElementById('total-payment');
const totalInterest = document.getElementById('total-interest');

const calculateInterest = document.getElementById('loan-form');

const loader = document.getElementById('loading');
const results = document.querySelector('#results');
const card2 = document.querySelector('.card2');

// Show error
const showError = function(error){
//show loader
loader.style.display = 'none';
card2.style.backgroundColor = "transparent";
const errorDiv = document.createElement('div');
const card = document.querySelector('.card1');
const heading = document.querySelector('.heading');

errorDiv.className = 'alert alert-danger mb-3';

errorDiv.appendChild(document.createTextNode(error));

card.insertBefore(errorDiv, heading);

setTimeout(function(){
document.querySelector('.alert').remove();
},3000)
}

// calculate function
const result = function(){

const principal = parseFloat(amount.value);
const calcInterest = parseFloat(interest.value) / 100 / 12;
const calcPayment = parseFloat(year.value) * 12;

//compute monthly payment
const x = Math.pow(1 + calcInterest, calcPayment);
const monthly = (principal * x * calcInterest) / (x -1);

if(isFinite(monthly)){
monthlyPayment.value = monthly.toFixed(2);
totalPayment.value = (monthly * calcPayment).toFixed(2);
totalInterest.value = ((monthly * calcPayment) - principal).toFixed(2);
//hide result
results.style.display = 'block';
//show loader
loader.style.display = 'none';
}
else{
showError('Input valid Amount');
}
}


// calculate event listener
calculateInterest.addEventListener('submit', function(e){
e.preventDefault();

card2.style.backgroundColor = "#fff";

//hide result
results.style.display = 'none';
//show loader
loader.style.display = 'block';

setTimeout(result, 2000)
});