Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 11 of 11
Thread: help with a for loop problem

10012008, 02:55 AM #1
 Join Date
 Oct 2008
 Posts
 7
 Thanks
 2
 Thanked 0 Times in 0 Posts
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?
10012008, 05:32 AM
#2
 Join Date
 Feb 2008
 Location
 Cebu City, Philippines
 Posts
 1,752
 Thanks
 65
 Thanked 372 Times in 365 Posts
++ stands for incrementeng the variable by 1.
if you have this:
For your problem, yhy not just multiply the "set amount" to an increasing variable.Code:for(var i=0;i<10;i++
)
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
Learn how to javascript at 02geek
The more you learn, the more you'll realize there's much more to learn
Ray.ph
10012008, 05:11 PM
#3
 Join Date
 Oct 2008
 Posts
 7
 Thanks
 2
 Thanked 0 Times in 0 Posts
Thanks for the help
10032008, 09:54 AM
#4
 Join Date
 Oct 2008
 Posts
 7
 Thanks
 2
 Thanked 0 Times in 0 Posts
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>
10042008, 01:18 AM
#5
 Join Date
 Dec 2007
 Posts
 6,682
 Thanks
 436
 Thanked 890 Times in 879 Posts
best regardsCode:<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>
10042008, 04:12 PM
#6
 Join Date
 Oct 2008
 Posts
 7
 Thanks
 2
 Thanked 0 Times in 0 Posts
what would the code be to increase the payment by 15 every 10 payments rather than just after 10
10042008, 04:24 PM
#7
 Join Date
 Dec 2007
 Posts
 6,682
 Thanks
 436
 Thanked 890 Times in 879 Posts
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:
if you post some numeric example, not the code, maybe I could understand and of course help with some code.Code:var sum = 0, i; for(i = 1; i < 31; i++){ sum += i; // this is same as sum = sum + 1 } alert(sum);
best regards
Users who have thanked oesxyl for this post:
CharlesB (10052008)
10042008, 06:48 PM
#8
 Join Date
 Oct 2008
 Posts
 7
 Thanks
 2
 Thanked 0 Times in 0 Posts
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
10062008, 12:08 AM
#9
 Join Date
 Dec 2007
 Posts
 6,682
 Thanks
 436
 Thanked 890 Times in 879 Posts
sorry for delay. If I understand what you said, this could be the code:
best regardsCode:/* 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(7,15,10)); // 7 payments, $15 alert(c_sum(14,15,10)); // 14 payments, 10*$15 + 4 * $30
Users who have thanked oesxyl for this post:
CharlesB (10072008)
10062008, 06:18 PM
#10
 Join Date
 Oct 2008
 Posts
 7
 Thanks
 2
 Thanked 0 Times in 0 Posts
thanks for your help. I am at work right now but I will look this over tonight when I get home.
10072008, 06:46 AM
#11
 Join Date
 Oct 2008
 Posts
 7
 Thanks
 2
 Thanked 0 Times in 0 Posts
Just tried the code. Great work!!! Thanks