Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    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?

  • #2
    Senior Coder rangana's Avatar
    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:
    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
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the help

  • #4
    New to the CF scene
    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>

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • #6
    New to the CF scene
    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

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by CharlesB View Post
    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
    }
    alert(sum);
    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)

  • #8
    New to the CF scene
    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

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by CharlesB View Post
    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(7,15,10)); // 7 payments, $15
        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)

  • #10
    New to the CF scene
    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.

  • #11
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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
    •