SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with simple loop.

    My code prints these numbers. 1 4 9 16 25 36 49 64 81 100. My problem is I've got to make it print new lines after the numbers 9,36,and 81 so it will look like this.
    1 4 9
    16 25 36
    49 64 81
    100

    I think I have to use the modulus because all the numbers before the next lines are divisible by 3.I'm not sure how to put it in the code.I've been noodling over this for some time now, and I know its gotta be something simple. Thanks, here's the code.I have to do this problem using a loop.

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    
    	var count = 1;
    	
    	while (count <= 10)
    	{
         document.write(count * count + " " );
        
          ++ count;	 
    	
    	}
    
    //-->
    </SCRIPT>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,448
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    newlines

    Hi KillMyComputer, welcome to the forums
    As you are incrementing the count variable, you can use a conditional to test for it's value and if-else the document.write() line to either use a "\n" or not.

  3. #3
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Guru, I'll give it a go.

  4. #4
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your the man Guru. Thanks. It seems like I always get stuck on the stupidest things.......C'est La Vie.


Bookmarks

Posting Permissions

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