Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: help with a for loop problem

1. help with a for loop problem

Hi, I am new to javascript and I am trying to create a script to calculate the sum of a stream of payments that are made every 2 weeks and increase each year by a set amount for a specified number of periods ie... \$30 every 2 weeks the first year, \$60 every two weeks the next year (or 26 payments), \$90 every two weeks the next year etc...

I think the for loop code is supposed to be used but I am not sure how. Is there an article or sample code that I can look at?

Also, what does ++ stand for?

• ++ stands for incrementeng the variable by 1.
if you have this:
Code:
`for(var i=0;i<10;`i++`)`
For your problem, yhy not just multiply the "set amount" to an increasing variable.

Multiply by 2 for the second year.
Multiply by 3 for the third year...and so on and so forth.

For Loop tut:
http://www.w3schools.com/js/js_loop_for.asp

• Thanks for the help

• 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.

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>

• Code:
```<script type="text/javascript">
<!--
var supp = 15
for(i = 0; i < 20; i++){
a = supp*i
n = supp+a // `because of previus line this will be supp*(i+1)`
if (a > (supp*10))  { // `this is true when supp*i > 10*supp => for any i > 10`
a = ((supp*2)*i)
n = supp+a // `same reason as above, supp*(2*i+1)`
}
}
```// n is not a sum, you assign a value for each i then forget it
// that means this will be, last i =20, 15*(2*20+1)```
document.write(n);
</script>```
best regards

• what would the code be to increase the payment by 15 every 10 payments rather than just after 10

• Originally Posted by CharlesB
what would the code be to increase the payment by 15 every 10 payments rather than just after 10
I don't understand what you want to achive that's why I only comment your code. If you want to sum all numbers from 1 to 30 this will be the code:
Code:
```var sum = 0, i;
for(i = 1; i < 31; i++){
sum += i; // this is same as sum = sum + 1
}
if you post some numeric example, not the code, maybe I could understand and of course help with some code.

best regards

• Users who have thanked oesxyl for this post:

CharlesB (10-05-2008)

• For example, I would like to be able to calculate the value of stream of payments that increases by a set amount after each 10 payments... ie 10 payments of \$15, then 10 payments of \$30, then 10 payments of \$45...etc. In this example the payments increase by \$15 each 10 payments. The variable is the number of payments

• Originally Posted by CharlesB
For example, I would like to be able to calculate the value of stream of payments that increases by a set amount after each 10 payments... ie 10 payments of \$15, then 10 payments of \$30, then 10 payments of \$45...etc. In this example the payments increase by \$15 each 10 payments. The variable is the number of payments
sorry for delay. If I understand what you said, this could be the code:

Code:
```    /* payments - number of payments
* amount - \$15
* delta - 10
*/
function c_sum(payments,amount,delta){
var i, tens, sum = 0;
for(i=0;i<payments;i++){
tens = 1 + (i - (i % delta))/delta;
sum += amount * tens;
}
return sum;
}
alert(c_sum(14,15,10)); // 14 payments, 10*\$15 + 4 * \$30```
best regards

• Users who have thanked oesxyl for this post:

CharlesB (10-07-2008)

• thanks for your help. I am at work right now but I will look this over tonight when I get home.

• Just tried the code. Great work!!! Thanks

•

Posting Permissions

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