1. Registered User
Join Date
May 2013
Posts
4

## JavaScript help

Hello,

I'm new here and I hope I'm in the correct category...

First, sorry for my bad english, I'm from switzerland.

I need help with JavaScript. I have copied this calculator but I can't change it.

HTML Code:
<script language ="JavaScript">
<!--
function calculate_finance() {

amount = document.getElementById('calc_amount').value.replace(',','.');
amount = amount.replace(/[^0-9\.]/g, '');

if (amount <= 99.99)		{var period = 30; var interest = 2.4; var deviation_percent = 2.4; }

else if (amount >= 100.00 && amount <= 999.99)	{var period = 60; var interest = 2.8; var deviation_percent = 2.8; }

else if (amount >= 1000.00 && amount <= 3000.00)	{var period = 90; var interest = 3.4; var deviation_percent = 3.4; }

compound = parseInt(document.getElementById('calc_compounding').value);
if (compound > 100) compound = 100;
else if ((compound < 0) || (isNaN(compound))) compound = 0;

document.getElementById('calc_compounding').value = compound;

var profit = this_profit = direct = this_direct = first_profit = last_profit = total_profit = deviation = 0;
for (i=0; i < period; i++) {
profit =  Math.round((amount*1 + this_profit*1) * interest)/100;
direct =  Math.round(profit*(1-compound*0.01)*100)/100;
if (first_profit == 0) first_profit = profit;
last_profit = profit;
profit -= direct;
this_profit += profit*1;
this_direct += direct*1;
}

total_profit = Math.round((this_profit*1 + this_direct*1)*10)/10;
total_profit = isNaN(total_profit)?0:total_profit;

daily_profit = Math.round(total_profit*10/period)/10;

total_return = Math.round(total_profit*10+amount*10)/10;

if (total_return < 1000) {
total_return = Math.round(total_profit*10+amount*10)/10;
deviation = Math.round(total_return*deviation_percent*0.5*10/100)/10;
}
else {
total_return = Math.round(total_profit*1+amount*1);
deviation = Math.round(total_return*deviation_percent*0.5/100);
}

deviation = deviation<0.1?0.1:deviation;

roi_min = Math.round((total_return-deviation)*1000/amount)/10;
roi_max = Math.round((total_return+deviation)*1000/amount)/10;
roi = isNaN(roi_max)?0:roi_min+' <small>%</small> - '+roi_max;

document.getElementById('calc_total_return').innerHTML = '<b>'+total_return+'</b> \$';
document.getElementById('calc_roi').innerHTML = '<b>'+roi+'</b> %';
document.getElementById('calc_period').innerHTML = '<b>'+period+'</b> '+ (period<100?'business ':'b/') + 'days';
//document.getElementById('calc_end_date').innerHTML = '<b>'+end_date+'</b>';
document.getElementById('calc_amount').value = amount;
if (deviation>0) document.getElementById('calc_deviation').innerHTML = '±<b>'+deviation+'</b> \$';
else document.getElementById('calc_deviation').innerHTML = '';

}

function save_calc_data() {
amount = document.getElementById('calc_amount').value.replace(',','.');
amount = amount.replace(/[^0-9\.]/g, '');
if (amount < 5) {
amount = 5;
document.getElementById('calc_amount').value = amount;
}
document.cookie="calc_amount="+amount+"; path=/; expires=Mon, 01-Jan-2018 00:00:00 GMT";
document.cookie="calc_compounding="+document.getElementById('calc_compounding').value +"; path=/; expires=Mon, 01-Jan-2018 00:00:00 GMT";
calculate_finance();
}
-->
</script>

<div id="bottom-container-3" class="container-col-33">
<img class="left ml-10" src="/img/bottom-container-calculator.png" height="38" width="42" alt="Financial calculator">
<h3>Financial Calculator</h3>

<div class="h-20"></div>

<div class="financial-calc">
<table>
<tr>
<td width=82><label>Investment<br >amount</label></td>
<td width=55><input type="text" id="calc_amount" onblur="calculate_finance();" onkeyup="javascript:this.value=this.value.replace(/[^0-9\.\,]/g, ''); calculate_finance();" onchange="save_calc_data();" value="100"></td>
<td width=10>&nbsp;</td>
<td width=70><label>Total<br />return</label></td>

<td class="data"><span id="calc_total_return"><b>118</b> \$</span> <span id="calc_deviation">±<b>2.4</b> \$</span></td>
</tr>
<tr>
<td><label>Compounding</label></td><td><input type="text" id="calc_compounding" onblur="calculate_finance();" onkeyup="javascript:this.value=this.value.replace(/[^0-9\.\,]/g, ''); calculate_finance();" onchange="save_calc_data();" value=""></td>
<td>&nbsp;</td>
<td><label>Period</label></td><td class="data"><span id="calc_period"><b>10</b> calendar days</span></td>
</tr>
<tr>
<td colspan=2>&nbsp;<!--<label>End date</label></td><td class="data"><span id="calc_end_date"><b></b></span>--></td>
<td>&nbsp;</td>
<td><label>ROI</label></td><td class="data"><span id="calc_roi"><b>160</b> %</span></td>
</tr>
</table>
<script language ="JavaScript">calculate_finance();</script>
</div>
</div>
</div></div>

Code:
document.getElementById('daily_profit').innerHTML = '<b>'+daily_profit+'</b> \$';
and this in the table:

HTML Code:
<tr>
<td colspan=2>&nbsp;<!--<label>End date</label></td><td class="data"><span id="calc_end_date"><b></b></span>--></td>
<td>&nbsp;</td>
<td><label>ROI</label></td><td class="data"><span id="daily_profit"><b>160</b> %</span></td>
</tr>
it will change the period to 10 days... I can't fix that. Any ideas?

2. what is "red text"?!

3. Registered User
Join Date
May 2013
Posts
4

4. i'm not sure if it works as expexted (i did't analize the code much and i'm not good in financial voodoo) so try this:

Code:
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Calculator</title>
<body>
<script language ="JavaScript">
function calculate_finance(){
var amount = document.getElementById('calc_amount').value.replace(',','.');
amount = amount.replace(/[^0-9\.]/g, '');
if(amount <= 99.99){var period = 30; var interest = 2.4; var deviation_percent = 2.4;}
else if(amount >= 100.00 && amount <= 999.99){var period = 60; var interest = 2.8; var deviation_percent = 2.8;}
else if(amount >= 1000.00 && amount <= 3000.00)	{var period = 90; var interest = 3.4; var deviation_percent = 3.4;}
var compound = parseInt(document.getElementById('calc_compounding').value);
if(compound > 100){compound = 100;}
else if((compound < 0) || (isNaN(compound))){compound = 0;}
document.getElementById('calc_compounding').value = compound;
var profit = this_profit = direct = this_direct = first_profit = last_profit = total_profit = deviation = 0;
for(var i=0; i < period; i++){
profit =  Math.round((amount*1 + this_profit*1) * interest)/100;
direct =  Math.round(profit*(1-compound*0.01)*100)/100;
if(first_profit == 0) first_profit = profit;
last_profit = profit;
profit -= direct;
this_profit += profit*1;
this_direct += direct*1;
}

var total_profit = Math.round((this_profit*1 + this_direct*1)*10)/10;
total_profit = isNaN(total_profit)?0:total_profit;

var daily_profit = Math.round(total_profit*10/period)/10;
var total_return = Math.round(total_profit*10+amount*10)/10;

if(total_return < 1000){
total_return = Math.round(total_profit*10+amount*10)/10;
deviation = Math.round(total_return*deviation_percent*0.5*10/100)/10;
}
else{
total_return = Math.round(total_profit*1+amount*1);
deviation = Math.round(total_return*deviation_percent*0.5/100);
}

deviation = deviation<0.1?0.1:deviation;

var roi_min = Math.round((total_return-deviation)*1000/amount)/10;
roi_max = Math.round((total_return+deviation)*1000/amount)/10;
roi = isNaN(roi_max)?0:roi_min+' <small>%</small> - '+roi_max;

document.getElementById('calc_total_return').innerHTML = '<b>'+total_return+'</b> \$';
document.getElementById('calc_roi').innerHTML = '<b>'+roi+'</b> %';
document.getElementById('calc_period').innerHTML = '<b>'+period+'</b> '+ (period<100?'business ':'b/') + 'days';
//document.getElementById('calc_end_date').innerHTML = '<b>'+end_date+'</b>';
document.getElementById('calc_amount').value = amount;
if (deviation>0){document.getElementById('calc_deviation').innerHTML = '±<b>'+deviation+'</b> \$';}
else{document.getElementById('calc_deviation').innerHTML = '';}
document.getElementById('daily_profit').innerHTML = '<b>'+daily_profit+'</b> \$';
}

function save_calc_data(){
var amount = document.getElementById('calc_amount').value.replace(',','.');
amount = amount.replace(/[^0-9\.]/g, '');
if(amount < 5){amount = 5;document.getElementById('calc_amount').value = amount;}
document.cookie="calc_amount="+amount+"; path=/; expires=Mon, 01-Jan-2018 00:00:00 GMT";
document.cookie="calc_compounding="+document.getElementById('calc_compounding').value +"; path=/; expires=Mon, 01-Jan-2018 00:00:00 GMT";
calculate_finance();
}

</script>

<div id="bottom-container-3" class="container-col-33">
<img class="left ml-10" src="/img/bottom-container-calculator.png" height="38" width="42" alt="Financial calculator">
<h3>Financial Calculator</h3>

<div class="h-20"></div>

<div class="financial-calc">
<table>
<tr>
<td width=82><label>Investment<br >amount</label></td>
<td width=55><input type="text" id="calc_amount" onblur="calculate_finance();" onkeyup="javascript:this.value=this.value.replace(/[^0-9\.\,]/g, ''); calculate_finance();" onchange="save_calc_data();" value="100"></td>
<td width=10>&nbsp;</td>
<td width=70><label>Total<br />return</label></td>

<td class="data"><span id="calc_total_return"><b>118</b> \$</span> <span id="calc_deviation">Ѽb>2.4</b> \$</span></td>
</tr>
<tr>
<td><label>Compounding</label></td><td><input type="text" id="calc_compounding" onblur="calculate_finance();" onkeyup="javascript:this.value=this.value.replace(/[^0-9\.\,]/g, ''); calculate_finance();" onchange="save_calc_data();" value=""></td>
<td>&nbsp;</td>
<td><label>Period</label></td><td class="data"><span id="calc_period"><b>10</b> calendar days</span></td>
</tr>
<tr>
<td colspan=2>&nbsp;<!--<label>End date</label></td><td class="data"><span id="calc_end_date"><b></b></span>--></td>
<td>&nbsp;</td>
<td><label>ROI</label></td><td class="data"><span id="calc_roi"><b>160</b> %</span></td>
</tr>
<tr>
<td colspan=2>&nbsp;</td>
<td>&nbsp;</td>
<td><label>Daily profit</label></td><td class="data"><span id="daily_profit">a million</span></td>
</tr>
</table>
<script language ="JavaScript">calculate_finance();</script>
</div>
</div>
</div></div>

</body></html>

5. Registered User
Join Date
May 2013
Posts
4
Yes it works Thank you!

Do you know how can I disable the +/- (amount) after total return and make the ROI exactly?

I mean, when I set the plan to:

2.8% daily after 60 days the ROI is 268 (including principal back (100% of your deposit)) but the calculator show ROI 264.3 % - 271.8 %

It's possible?

6. i believe it is. but when i start trying to understand what's goin on in the code and see all these last/first/profit/profitprofit i feel my brain is about to boil or blow lol i have no any idea what does that 'roi' mean this makes all my nightmares come true. sorry i can't understand the financial abracadabra and the logic of the script ((( i think somebody else can easily change the script to fit your needs

7. Registered User
Join Date
May 2013
Posts
4
who know the solution?