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?

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.38985 seconds
• Memory Usage 2,941KB
• Queries Executed 15 (?)
Template Usage (34):
• (2)bbcode_code
• (2)bbcode_html
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (7)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (7)postbit
• (7)postbit_onlinestatus
• (7)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (71):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete