/    Sign up×
Community /Pin to ProfileBookmark

calculating numbers with a periodic increase

I am still having trouble getting the script right. If I am trying to calculate the sum of a stream of payment that start at $15 each but then increase to $30 after 10 payments are made what am I doing wrong? I know that the total should come out to 450 (i < 20).

Here is the script;

<script type=”text/javascript”>
<!–
var supp = 15

for(i = 0; i < 20; i++){

a = supp*i
n = supp+a

if (a > (supp*10))
{
a = ((supp*
2)*i)
n = supp+a
}
}
document.write(n);
</script>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisOct 03.2008 — <i>
</i>&lt;script type="text/javascript"&gt;
var supp = 15
n = 0
for(i=0; i&lt;20; i++)
{if (i == 10) {supp = 30}
n = n + supp}
document.write(n);
&lt;/script&gt;
Copy linkTweet thisAlerts:
@CharlesBauthorOct 03.2008 — Thank you, your help is much appreciated?
Copy linkTweet thisAlerts:
@CharlesBauthorOct 03.2008 — Gil, if I want the payments to increase by 15 after every 10 payments what type of loop would that be? for or while? I think I have to divide the number of times the loop runs by 10 and then make a separate for statement to do each calculation and sum the total of each. Does that make sense?
Copy linkTweet thisAlerts:
@gil_davisOct 06.2008 — &lt;script type="text/javascript"&gt;
var supp = 0 // the first time will still be 15
n = 0
nPmt = 50; // change for how many payments you want
for(i=0; i&lt;nPmt; i++)
{if ((i &amp;#37; 10) == 0) {supp = supp + 15}
n = n + supp}
document.write(n);
&lt;/script&gt;
Copy linkTweet thisAlerts:
@CharlesBauthorOct 06.2008 — Thank you for your help! I don't have a chance to review the code right now as I am at work, but tonight I will. Again I really appreciate this help ?
Copy linkTweet thisAlerts:
@CharlesBauthorOct 07.2008 — Thank you Gil, the code works great!!
×

Success!

Help @CharlesB 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...