/    Sign up×
Community /Pin to ProfileBookmark

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.

[code=html]<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″>
<div class=”padding-10″>
<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]

I want to add this (red text) but when I add:

[CODE]document.getElementById(‘daily_profit’).innerHTML = ‘<b>’+daily_profit+'</b> $’;
[/CODE]

and this in the table:

[code=html]<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>
[/code]

it will change the period to 10 days… I can’t fix that. Any ideas?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@PadonakMay 15.2013 — what is "red text"?!
Copy linkTweet thisAlerts:
@PadonakMay 16.2013 — 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:

<i>
</i>&lt;!DOCTYPE html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
&lt;title&gt;Calculator&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script language ="JavaScript"&gt;
function calculate_finance(){
var amount = document.getElementById('calc_amount').value.replace(',','.');
amount = amount.replace(/[^0-9.]/g, '');
if(amount &lt;= 99.99){var period = 30; var interest = 2.4; var deviation_percent = 2.4;}
else if(amount &gt;= 100.00 &amp;&amp; amount &lt;= 999.99){var period = 60; var interest = 2.8; var deviation_percent = 2.8;}
else if(amount &gt;= 1000.00 &amp;&amp; amount &lt;= 3000.00) {var period = 90; var interest = 3.4; var deviation_percent = 3.4;}
var compound = parseInt(document.getElementById('calc_compounding').value);
if(compound &gt; 100){compound = 100;}
else if((compound &lt; 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 &lt; 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 &lt; 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&lt;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+' &lt;small&gt;%&lt;/small&gt; - '+roi_max;

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

function save_calc_data(){
var amount = document.getElementById('calc_amount').value.replace(',','.');
amount = amount.replace(/[^0-9.]/g, '');
if(amount &lt; 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();
}

&lt;/script&gt;

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

<i> </i> &lt;div class="h-20"&gt;&lt;/div&gt;

<i> </i> &lt;div class="financial-calc"&gt;
<i> </i> &lt;table&gt;
<i> </i> &lt;tr&gt;
<i> </i> &lt;td width=82&gt;&lt;label&gt;Investment&lt;br &gt;amount&lt;/label&gt;&lt;/td&gt;
<i> </i> &lt;td width=55&gt;&lt;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"&gt;&lt;/td&gt;
<i> </i> &lt;td width=10&gt;&amp;nbsp;&lt;/td&gt;
<i> </i> &lt;td width=70&gt;&lt;label&gt;Total&lt;br /&gt;return&lt;/label&gt;&lt;/td&gt;

<i> </i> &lt;td class="data"&gt;&lt;span id="calc_total_return"&gt;&lt;b&gt;118&lt;/b&gt; $&lt;/span&gt; &lt;span id="calc_deviation"&gt;&amp;#1148;b&gt;2.4&lt;/b&gt; $&lt;/span&gt;&lt;/td&gt;
<i> </i> &lt;/tr&gt;
<i> </i> &lt;tr&gt;
<i> </i> &lt;td&gt;&lt;label&gt;Compounding&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;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=""&gt;&lt;/td&gt;
<i> </i> &lt;td&gt;&amp;nbsp;&lt;/td&gt;
<i> </i> &lt;td&gt;&lt;label&gt;Period&lt;/label&gt;&lt;/td&gt;&lt;td class="data"&gt;&lt;span id="calc_period"&gt;&lt;b&gt;10&lt;/b&gt; calendar days&lt;/span&gt;&lt;/td&gt;
<i> </i> &lt;/tr&gt;
<i> </i> &lt;tr&gt;
<i> </i> &lt;td colspan=2&gt;&amp;nbsp;&lt;!--&lt;label&gt;End date&lt;/label&gt;&lt;/td&gt;&lt;td class="data"&gt;&lt;span id="calc_end_date"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;--&gt;&lt;/td&gt;
<i> </i> &lt;td&gt;&amp;nbsp;&lt;/td&gt;
<i> </i> &lt;td&gt;&lt;label&gt;ROI&lt;/label&gt;&lt;/td&gt;&lt;td class="data"&gt;&lt;span id="calc_roi"&gt;&lt;b&gt;160&lt;/b&gt; %&lt;/span&gt;&lt;/td&gt;
<i> </i> &lt;/tr&gt;
<i> </i> &lt;tr&gt;
<i> </i> &lt;td colspan=2&gt;&amp;nbsp;&lt;/td&gt;
<i> </i> &lt;td&gt;&amp;nbsp;&lt;/td&gt;
<i> </i> &lt;td&gt;&lt;label&gt;Daily profit&lt;/label&gt;&lt;/td&gt;&lt;td class="data"&gt;&lt;span id="daily_profit"&gt;a million&lt;/span&gt;&lt;/td&gt;
<i> </i> &lt;/tr&gt;
<i> </i> &lt;/table&gt;
<i> </i> &lt;script language ="JavaScript"&gt;calculate_finance();&lt;/script&gt;
<i> </i> &lt;/div&gt;
<i> </i>&lt;/div&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;/body&gt;&lt;/html&gt;
Copy linkTweet thisAlerts:
@DogHamauthorMay 16.2013 — 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 [COLOR="#FF0000"]ROI 264.3 % - 271.8 %[/COLOR]

It's possible?
Copy linkTweet thisAlerts:
@PadonakMay 16.2013 — 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
Copy linkTweet thisAlerts:
@DogHamauthorMay 16.2013 — who know the solution?
×

Success!

Help @DogHam spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...