Results 1 to 3 of 3

Thread: What does this javascript code does? i don't understand it. Thanks

Hybrid View

  1. #1
    Join Date
    Oct 2012

    What does this javascript code does? i don't understand it. Thanks

    var sum = 0;
    for (fl=10; fl<=150; fl = fl + 5)
    sum = sum + fl;
    document.write (sum = sum + fl);

  2. #2
    Join Date
    Sep 2011
    Bristol, England, United Kingdom

    The script is a for loop. The loop parameters are as follows:

    1. Declaration & initialisation
    2. Logical check
    3. Increment/decrement

    In other words, the 1st parameter of the loop declares and initialises a variable (fl) to 10. The loop then executes the 2nd parameter, which is a logical check. It basically says "if FL is less-than-or-equal-to 150". If this statement is a true statement, it will execute the body of the loop (sum = sum + fl) and will then proceed to the 3rd and final parameter of the loop, which is an increment.

    This code can also be written as:

    var sum = 0;
    for (fl=10; fl<=150; fl+= 5)
        sum += fl;
    document.write (sum += fl);
    This code doesn't really make much sense, but it may be part of a larger file that we can't see.

  3. #3
    Join Date
    Oct 2012

    Thumbs up Hi

    If you run the script the output would be 2475

    for (fl = 10; fl <= 150; fl = fl + 5) This means that you give the value to fl =10 and to add 5 as long as fl value<=150

    that means fl = 10,15,20,25.........155

    when you have sum=0 that means new sum=0+10, sum=10+15, sum=25+20, sum=55+25

    and finally the new sum would be 2475

    I think this one ill clearly you question.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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