View Full Version : help with a for loop problem

CharlesB

10-01-2008, 02:55 AM

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?

rangana

10-01-2008, 05:32 AM

++ stands for incrementeng the variable by 1.

if you have this:

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

CharlesB

10-01-2008, 05:11 PM

Thanks for the help

CharlesB

10-03-2008, 09:54 AM

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>

oesxyl

10-04-2008, 01:18 AM

<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

CharlesB

10-04-2008, 04:12 PM

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

oesxyl

10-04-2008, 04:24 PM

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:

var sum = 0, i;

for(i = 1; i < 31; i++){

sum += i; // this is same as sum = sum + 1

}

alert(sum);

if you post some numeric example, not the code, maybe I could understand and of course help with some code.

best regards

CharlesB

10-04-2008, 06:48 PM

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

oesxyl

10-06-2008, 12:08 AM

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:

/* 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

best regards

CharlesB

10-06-2008, 06:18 PM

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

CharlesB

10-07-2008, 06:46 AM

Just tried the code. Great work!!! Thanks:thumbsup:

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.