I have been implementing an EMI calculator for a hybrid mobile application and within one of the loops the execution takes too long and ultimately the page goes unresponsive. Here is my code
var EMICalculator = {
basicEMI: function (amount, rate, tenure) {
// body...
var rate_yearly = parseFloat((rate/12)/100);
var amt = parseFloat(amount*rate_yearly*Math.pow((rate_yearly+1),tenure)/(Math.pow((rate_yearly+1),tenure)-1));
return amt.toFixed(2);
},
getBalanceClassic: function (amount, rate, tenure, emi, openingBal, toReturn){
var interest_per_month = 0.0;
var schedule = [];
for(var i=1; i<=tenure; i++){
var dataJson = {};
interest_per_month = amount*rate;
amount = parseFloat(amount - emi - interest_per_month);
dataJson['installment_no'] = i;
dataJson['installment'] = Math.round(interest_per_month + (emi - interest_per_month)).toFixed(2);
dataJson['interest'] = Math.round(interest_per_month).toFixed(2);
dataJson['principal'] = Math.round(emi - interest_per_month).toFixed(2);
dataJson['balance_principal'] = Math.round(amount).toFixed(2);
dataJson['opening_balance'] = Math.round(openingBal).toFixed(2);
amt_initially = Math.round(amount).toFixed(2);
schedule.push(dataJson);
}
if(toReturn){
return schedule;
}else {
return parseFloat(amount);
}
},
rateBasedClassic: function (amount, rate, tenure) {
var rate_per_yr = parseFloat((rate/12)/100);
var amt_initially = amount;
var emi_basic = parseFloat(this.basicEMI(amount,rate,tenure));
var total_interest = Math.round((emi_basic*tenure)-amount);
var total_amount = Math.round(emi_basic*tenure);
var interest_per_month = Math.round(total_interest/tenure);
var amount_paid = 0.0;
var toReturn = [];
for(var i=1; i<=tenure; i++){
var dataJson = {};
amount_paid = parseFloat(amount_paid + emi_basic);
interest_per_month = amount * rate_per_yr;
amount = (amount - (emi_basic - interest_per_month));
dataJson['installment_no'] = i;
dataJson['installment'] = Math.round(interest_per_month + (emi_basic - interest_per_month)).toFixed(2);
dataJson['interest'] = Math.round(interest_per_month).toFixed(2);
dataJson['principal'] = Math.round(emi_basic - interest_per_month).toFixed(2);
dataJson['balance_principal'] = Math.round(amount).toFixed(2);
dataJson['opening_balance'] = Math.round(amt_initially).toFixed(2);
amt_initially = Math.round(amount).toFixed(2);
toReturn.push(dataJson);
}
return toReturn;
},
EMIBasedClassic : function (amount, tenure, emi) {
var amt_initially = amount;
var total_amount = emi*tenure;
var total_interest = total_amount - amount;
var rate = 0.0;
var toReturn = false;
var balance = this.getBalanceClassic(amount,rate,tenure,emi,amt_initially,toReturn);
while(balance<0){
rate += 0.1;
var rate2 = parseFloat((rate/12)/100);
balance = this.getBalanceClassic(amount,rate2,tenure,emi,amt_initially,toReturn);
}
while(balance>0){
rate -= 0.0000001;
var rate2 = parseFloat((rate/12)/100);
balance = this.getBalanceClassic(amount,rate2,tenure,emi,amt_initially,toReturn);
}
toReturn = true;
var rate2 = parseFloat((rate/12)/100);
balance = this.getBalanceClassic(amount,rate2,tenure,emi,amt_initially,toReturn);
return balance;
}}
Calling the EMIBasedClassic method with parameters (600000,9,19080) causes the issue where the incremental increase/decrease has been done in the while loops. The same logic is working fine in Java. What's wrong here?
When you call getBalance classic, you're using the same parameters every single time. That means balance, will always be the same value. I'm not sure which value you want to pass to the function, but the current setup will always run an infinite loop no matter what value you pass. It'd be super easy to over look such a minor thing.
function (amount, tenure, emi) {
var amt_initially = amount;
var total_amount = emi*tenure;
var total_interest = total_amount - amount;
var rate = 0.0;
var toReturn = false;
var balance = this.getBalanceClassic(amount,rate,tenure,emi,amt_initially,toReturn);
while(balance<0){
rate += 0.1;
var rate2 = parseFloat((rate/12)/100);
balance = this.getBalanceClassic(amount,rate2,tenure,emi,amt_initially,toReturn);
}
while(balance>0){
console.log("__BALANCE__: ", balance);
rate -= 0.0000001;
var rate2 = parseFloat((rate/12)/100);
//Balance will always be the same. Do we want to pass balance or amount?
balance = this.getBalanceClassic(amount,rate2,tenure,emi,amt_initially,toReturn);
}
toReturn = true;
var rate2 = parseFloat((rate/12)/100);
balance = this.getBalanceClassic(amount,rate2,tenure,emi,amt_initially,toReturn);
return balance;
}
You only want to use amount in the function on the very first time. After that, you want to use balance.
I changed it from amount to balance in the while loops and it worked.
Here's my code in repl:
This question already has answers here:
How can I check whether a radio button is selected with JavaScript?
(29 answers)
Closed 8 years ago.
Hi everyone can someone please help me with the following problem.
// i have written the problem inside the javascript
See http://jsfiddle.net/7Cmwc/3/ for example.
function calculate()
//If radiobutton with ID box3 is checked do this mybox1*mybox2+5
{
var myBox1 = document.getElementById('box1').value;
var myBox2 = document.getElementById('box2').value;
var result = document.getElementById('result');
var myResult = myBox1 * myBox2 + 5 ;
result.value = myResult;
}
//If radiobutton with ID box4 is checked do this mybox1*mybox2+10
{
var myBox1 = document.getElementById('box1').value;
var myBox2 = document.getElementById('box2').value;
var result = document.getElementById('result');
var myResult = myBox1 * myBox2 + 10 ;
result.value = myResult;
}
//If radiobutton with ID box3 is checked do this mybox1*mybox2+15
{
var myBox1 = document.getElementById('box1').value;
var myBox2 = document.getElementById('box2').value;
var result = document.getElementById('result');
var myResult = myBox1 * myBox2 + 15 ;
result.value = myResult;
}
And also i wonder what the difference is between jradiobutton and radiobutton?
Thanks in advance
Not saying I would do it this say, but this is a simple example to show you how it's done, at the very basic level.
function calculate() {
var myBox1 = document.getElementById('box1').value,
myBox2 = document.getElementById('box2').value,
myBox3 = document.getElementById('box3').checked,
myBox4 = document.getElementById('box4').checked,
myBox5 = document.getElementById('box5').checked,
result = document.getElementById('result'),
myResult;
if ( myBox3 ) {
myResult = myBox1 * myBox2 + 5 ;
result.value = myResult;
}
else if ( myBox4 ) {
myResult = myBox1 * myBox2 + 10 ;
result.value = myResult;
}
else if ( myBox5 ) {
myResult = myBox1 * myBox2 + 15 ;
result.value = myResult;
}
}
Like this...
function calculate() {
var test = document.getElementsByName("radioGroupName")
var sizes = test.length;
alert(sizes);
for (i = 0; i < sizes; i++) {
if (test[i].checked == true) {
whichIsCheckedValue = test[i].value;
whichIsChecked = i;
}
}
}
Once you have that, you can run your conditions. Of course, there is a better way, if you look at how this is done. You should have your conditions in the same place that it's identifying what was checked.
I am absolutely stumped, I have been working on this for weeks and have come to the conclusion that my knowledge is too limited to figure this out
ok, I have a page in my customer portal where employees can enter end of day sales reports. The first half of the page is input fields with dollar amounts and profits calculated by a percentage. Funny thing is it works on my website but not on jsfiddle.
the second part is a devices sold section using selects and calculations for price, cost and profit
there are 2 parts to the script and it gets crazy when I put them together, they dont work at all
I tried to post the entire page and script here but it was too long
here is the script
function Adder() {
var ppc = parseFloat(document.form.ppc.value);
var tmo = parseFloat(document.form.tmo.value);
var cf = parseFloat(document.form.cf.value);
var act = parseFloat(document.form.act.value);
var fla = parseFloat(document.form.fla.value);
var misc = parseFloat(document.form.misc.value);
var exp = parseFloat(document.form.exp.value);
var prof1 = parseFloat(document.form.prof1.value);
var prof2 = parseFloat(document.form.prof2.value);
var prof3 = parseFloat(document.form.prof3.value);
var prof4 = parseFloat(document.form.prof4.value);
var prof5 = parseFloat(document.form.prof5.value);
var result1 = ppc * 0.12;
document.form.prof1.value = result1.toFixed(2);
var result2 = tmo * 0.015;
document.form.prof2.value = result2.toFixed(2);
var result3 = cf * 0.21;
document.form.prof3.value = result3.toFixed(2);
var result4 = act * 35;
document.form.prof4.value = result4.toFixed(2);
var result5 = fla * 60;
document.form.prof5.value = result5.toFixed(2);
var result6 = ppc + tmo + cf + prof4 + prof5 + misc - exp;
document.form.sum1.value = result6.toFixed(2);
var result7 = prof1 + prof2 + prof3 + prof4 + prof5 + misc - exp;
document.form.total1.value = result7.toFixed(2);
}
var item = document.getElementById('item');
var item1 = document.getElementById('item1');
var item2 = document.getElementById('item2');
var item3 = document.getElementById('item3');
var item4 = document.getElementById('item4');
var item5 = document.getElementById('item5');
var item6 = document.getElementById('item6');
item.onchange = function () {
price.innerHTML = "$" + this.value;
cost.innerHTML = "$" + (this[this.selectedIndex].getAttribute('value2'));
qty.value = 1; //Order 1 by default.
add();
};
qty.onchange = function () {
add();
};
item1.onchange = function () {
price1.innerHTML = "$" + this.value;
cost1.innerHTML = "$" + (this[this.selectedIndex].getAttribute('value2'));
qty1.value = 1; //Order 1 by default.
add();
};
qty1.onchange = function () {
add();
};
item2.onchange = function () {
price2.innerHTML = "$" + this.value;
cost2.innerHTML = "$" + (this[this.selectedIndex].getAttribute('value2'));
qty2.value = 1; //Order 1 by default.
add();
};
qty2.onchange = function () {
add();
};
item3.onchange = function () {
price3.innerHTML = "$" + this.value;
cost3.innerHTML = "$" + (this[this.selectedIndex].getAttribute('value2'));
qty3.value = 1; //Order 1 by default.
add();
};
qty3.onchange = function () {
add();
};
item4.onchange = function () {
price4.innerHTML = "$" + this.value;
cost4.innerHTML = "$" + (this[this.selectedIndex].getAttribute('value2'));
qty4.value = 1; //Order 1 by default.
add();
};
qty4.onchange = function () {
add();
};
item5.onchange = function () {
price5.innerHTML = "$" + this.value;
cost5.innerHTML = "$" + (this[this.selectedIndex].getAttribute('value2'));
qty5.value = 1; //Order 1 by default.
add();
};
qty5.onchange = function () {
add();
};
item6.onchange = function () {
price6.innerHTML = "$" + this.value;
cost6.innerHTML = "$" + (this[this.selectedIndex].getAttribute('value2'));
qty6.value = 1; //Order 1 by default.
add();
};
qty6.onchange = function () {
add();
};
function add() {
var inputs = document.getElementsByTagName('input');
var selects = document.getElementsByTagName('select');
var total = 0;
var taxes = 0;
for (var i = 0; i < selects.length; i++) {
var sum = 0;
var price = (parseFloat(selects[i].value)) ? parseFloat(selects[i].value) : 0;
var cost = (parseFloat(selects[i][selects[i].selectedIndex].getAttribute('value2'))) ? parseFloat(selects[i][selects[i].selectedIndex].getAttribute('value2')) : 0;
var qty = (parseFloat(inputs[i].value)) ? parseFloat(inputs[i].value) : 0;
sum += (price - cost) * qty;
total += sum;
if (i === 0) {
document.getElementById('result').innerHTML = "$" + sum.toFixed(2);
} else {
document.getElementById('result' + i).innerHTML = "$" + sum.toFixed(2);
}
}
document.getElementById('total').innerHTML = "$" + total.toFixed(2);
}
and the jsfiddle for everything together, I just cant figure it out
am using the following code to add text box values if any of the textbox is not given value it is showing NaN in total textbox how to avoid it
<script>
function totalValue()
{
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;
var c = document.getElementById("c").value;
var d = parseFloat(a)+ parseFloat(b)+ parseFloat(c);
document.getElementById("total").value = d;
}
</script>
Try this:
<script>
function totalValue()
{
var a = document.getElementById("a").value || 0;
var b = document.getElementById("b").value || 0;
var c = document.getElementById("c").value || 0;
var d = parseFloat(a)+ parseFloat(b)+ parseFloat(c);
document.getElementById("total").value = d;
}
</script>
Fiddle: http://jsfiddle.net/AvzwM/
You can append a 0 to a string:
<script>
function totalValue()
{
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;
var c = document.getElementById("c").value;
var d = parseFloat('0' + a)+ parseFloat('0' + b)+ parseFloat('0' + c);
document.getElementById("total").value = d;
}
</script>
<script>
//checks if given text in text box is a number or unwanted string
filterInt = function (value)
{
if(/^\-?[0-9]+$/.test(value))
return Number(value);
return NaN;
}
function totalValue()
{
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;
var c = document.getElementById("c").value;
a=filterInt(a)||0; //if it is a string, convert it to a Nan and then if NaN, convert to zero
b=filterInt(b)||0;
c=filterInt(c)||0;
var d = parseFloat(a)+ parseFloat(b)+ parseFloat(c);
document.getElementById("total").value = d;
}
I want the output of my program to be sent to an MEdit box but for whatever reason the html method does not set the value of the box should I use an MLabel instead?
here is my code:
function MButton1Click(event)
{
var temp = $("#MEdit1").val();
temp = parseInt(temp);
var selectedItem = $("input[name='MRadioGroup1']:checked").val();
if(selectedItem == 0)
{
var five9ths = 5/9;
temp = temp - five9ths;
temp = temp + 32;
$('#MEdit3').html("C");
}
else
{
var nine5ths = 9/5;
temp = temp * nine5ths;
temp = temp + 32;
$('#MEdit3').html("F");
}
var temperature = temp.toString();
$('#MEdit2').html(temperature);
}
You need to use .val() instead of .html()
$('#MEdit3').val("C");
Here is what your code should look like :
function MButton1Click(event) {
var temp = $("#MEdit1").val();
temp = parseInt(temp);
var selectedItem = $("input[name='MRadioGroup1']:checked").val();
if (selectedItem == 0) {
var five9ths = 5 / 9;
temp = temp - five9ths;
temp = temp + 32;
$('#MEdit3').val("C");
} else {
var nine5ths = 9 / 5;
temp = temp * nine5ths;
temp = temp + 32;
$('#MEdit3').val("F");
}
var temperature = temp.toString();
$('#MEdit2').val(temperature);
}