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 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    javascript code help!

    I have written the code below myself and im trying to figure out if this is the outcome in a web browser => 0 1 2 3 4 5 6 7 8 9
    if not what would be the outcome?

    <script language=javascript type="text/javascript">
    for (i=0 ;i<10 ;i++) {
    document.write(i)
    }
    </script>


    and how would you modify this code so it prints only even numbers and how would you modify it so that it sums all values of i and prints the final sum?
    Last edited by university; 07-30-2011 at 05:20 PM.

  • #2
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    614
    Thanks
    48
    Thanked 64 Times in 64 Posts
    First, surround your code in the code tags. It makes it easier to read.

    Instead of trying to figure out what random bits of code mean, maybe you should read through a tutorial like this one. The i++ in your function adds 1 to i every time it loops. What you would want to do is change that to i+=2, which will add 2 each time (thus you will only get even numbers).

    To change what is output, do the same for document.write(i). Just change i to a formula that will get it to do what you want.
    "Yeah science!"
    Online Science Tools

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    university - It is not really in your best interests that others do all or most of your homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase. Finally, there is a limit to the number of times that you can take your pitcher to this well!

    As I have indicated before, if you show us the code you yourself have written we will be happy to correct or improve it, but you must make a genuine effort yourself. Have a look at forum rule #1.5.

    It is your responsibility to die() if necessary….. - PHP Manual

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    Jul 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    university - It is not really in your best interests that others do all or most of your homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase. Finally, there is a limit to the number of times that you can take your pitcher to this well!

    As I have indicated before, if you show us the code you yourself have written we will be happy to correct or improve it, but you must make a genuine effort yourself. Have a look at forum rule #1.5.

    It is your responsibility to die() if necessary….. - PHP Manual
    umm HELLO i have given the code a try as u can see above i am askin if its right lol!!

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by university View Post
    umm HELLO i have given the code a try as u can see above i am askin if its right lol!!
    No, it is not right, but you could have found that out for yourself by running it and looking at your error console, or better still Firebug.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by university View Post
    umm HELLO i have given the code a try as u can see above i am askin if its right lol!!
    You should be a tiny bit more respectful to those who are trying to help you with your HOMEWORK.

    And yeah maybe you posted 5 minutes worth of code in THIS thread, but in many others you have not done anything except ask for a handout.

    And the most important thing you have been told by Philip M is that you need to learn this yourself. Proudly handing in homework that someone else did for you may get you an A once, but from that point on you will fall behind.

    Homework is no fun, but you have to do it. We all went through it and we're not dead yet!
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #7
    New Coder
    Join Date
    Jul 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Krupski View Post
    You should be a tiny bit more respectful to those who are trying to help you with your HOMEWORK.

    And yeah maybe you posted 5 minutes worth of code in THIS thread, but in many others you have not done anything except ask for a handout.

    And the most important thing you have been told by Philip M is that you need to learn this yourself. Proudly handing in homework that someone else did for you may get you an A once, but from that point on you will fall behind.

    Homework is no fun, but you have to do it. We all went through it and we're not dead yet!
    i was talking to philip not u so where did u come out of lol and plus it would make sense if u helped but saying that is not helping me now is it!...and again paople this is NOT homework...and thanks philip for letting me know guess i have to keep trying lol!

  • #8
    New Coder
    Join Date
    Jul 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by djh101 View Post
    First, surround your code in the code tags. It makes it easier to read.

    Instead of trying to figure out what random bits of code mean, maybe you should read through a tutorial like this one. The i++ in your function adds 1 to i every time it loops. What you would want to do is change that to i+=2, which will add 2 each time (thus you will only get even numbers).

    To change what is output, do the same for document.write(i). Just change i to a formula that will get it to do what you want.
    thanks for the help ..atleast u taught me something new!

  • #9
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by university View Post
    i was talking to philip not u so where did u come out of lol and plus it would make sense if u helped but saying that is not helping me now is it!.
    Where did I come out of? Make sense if I helped?

    I replied to several of your other posts with information, advice and code samples. I guess you didn't bother to read them.

    From your attitude, I'm guessing you are around 18 to 20... I take comfort knowing that eventually you will grow up and not smart-mouth people who are trying to help you.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Krupski View Post
    From your attitude, I'm guessing you are around 18 to 20... I take comfort knowing that eventually you will grow up and not smart-mouth people who are trying to help you.
    university - one of the best ways to guarantee that people on this forum ignore you in future is to be rude and big-mouthed. This forum is not some juvenile chat-room like Facebook. You are a mendicant asking for people to give you their voluntary time and expertise. Think on!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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