JavaScript Calculators: Simple & Compound Interest, Postfix Evaluation
Simple and Compound Interest Calculator
Develop a simple and compound interest calculator using HTML, CSS, and JavaScript.
Enter principal amount: | |
Enter time period: | |
Enter rate of interest (%): |
Simple and Compound Interest Calculator (jQuery)
Develop a simple and compound interest calculator using HTML, CSS, and jQuery.
Simple-Compound-Interest-Calculator-JQ.html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#calculate').click(function(){
var p,t,r,si,ci,sip,cip;
p = $("#principal").val();
t = $("#period").val();
r = $("#interest").val();
si = parseInt((p*t*r)/100 );
amount = p*Math.pow((1 +r/100),t );
ci = amount-p;
sip = parseInt(si)+parseInt(p);
cip = parseFloat(ci)+parseFloat(p);
$('#res1').html("<font size=4 face=verdana color=green>Simple Interest : "+si+"</font>");
$('#res2').html("<font size=4 face=verdana color=green>Compound Interest : "+ci+"</font>");
$('#res3').html("<font size=4 face=verdana color=green>Simple Interest with Principal Amount : "+sip+"</font>");
$('#res4').html("<font size=4 face=verdana color=green>Compound Interest with Principal Amount : "+cip+"</font>");
});
});
</script>
Postfix Expression Evaluation
Develop an application for evaluating postfix expressions using HTML, CSS, and Object-Oriented Concepts (OOCs) in JavaScript.
Postfix-Evaluation-JS.html
JavaScript Code:
// Stack class
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length == 0;
}
printStack() {
var str = "";
for (var i = 0; i < this.items.length; i++)
str += this.items[i] + " ";
return str;
}
pop() {
if (this.items.length == 0)
return "Underflow";
return this.items.pop();
}
}
// Performs Postfix Evaluation on a given exp
function postFixEvaluation(exp) {
var stack = new Stack();
for (var i = 0; i < exp.length; i++) {
var c = exp[i];
if (!isNaN(c))
stack.push(c - '0');
else {
var val1 = stack.pop();
var val2 = stack.pop();
if (val1 == "Underflow" || val2 == "Underflow")
return "Can't perform postfix evaluation";
switch (c) {
case '+':
stack.push(val2 + val1);
break;
case '-':
stack.push(val2 - val1);
break;
case '/':
stack.push(val2 / val1);
break;
case '*':
stack.push(val2 * val1);
break;
}
}
}
return stack.pop();
}
function postfixevaluate() {
var ex, re;
ex = document.getElementById("postfix").value;
re = postFixEvaluation(ex);
document.getElementById('res').innerHTML = "Evaluation of Postfix Expression <font size=6 face=verdana color=green>" + ex + "</font> is <font size=6 face=verdana color=green>" + re + "</font>";
}