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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple javascript problem

    Code:
     <html>
    <head>
    <script type="text/javascript" language="javascript">
    function star(c){
    	var i;
    	for(i=0;i<=c;i++){
    		if(i<=5)
    			document.write('*');
    	}document.write("<hr");
    }
    </script>
    </head>
    <body>
    <script type="text/javascript" language="javascript">
    var i;
    for(i=0;i<=10;i++){
    	document.write("("+i+")");
    	star(i);
    }
    </script>
    </body>
    </html>
    Is there something wrong with my code? The output produced is not the same as i expected. Please help. Thank you.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by beatc View Post
    Code:
     <html>
    <head>
    <script type="text/javascript" language="javascript">
    function star(c){
    	var i;
    	for(i=0;i<=c;i++){
    		if(i<=5)
    			document.write('*');
    	}document.write("<hr");
    }
    </script>
    </head>
    <body>
    <script type="text/javascript" language="javascript">
    var i;
    for(i=0;i<=10;i++){
    	document.write("("+i+")");
    	star(i);
    }
    </script>
    </body>
    </html>
    Is there something wrong with my code? The output produced is not the same as i expected. Please help. Thank you.
    Yes, there is something wrong with the code. What output did you expect? Have you tried using your error console? Have you tried inserting alerts at strategic positions to inpect the values of variables?

    <script language="javascript"> is long obsolete.

    document.write() has been obsolete since Netscape 3 passed away 10+ years ago. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page.

    Hint: create a global variable named result = "", then at each step use result += whatever. Use result += "<br>" to create the line breaks. Then at the end invoke document.write once to display the result.

    We wil not do your homework for you, but I hope this has given you a push forward.

    "It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so." - Mark Twain, US humorist, novelist, short story author, & wit (1835 - 1910)
    Last edited by Philip M; 03-23-2013 at 09:51 AM.

    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.

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i expected the output to show the var i and result after the function star(i) is called after the each loop is executed. However, the output i got is different...is there any type on my code or what. From my logical point of view, this code should produce the one i mentioned. Please help. Thanx.

  • #4
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for double post but i don't see any EDIT post option. BTW, i solved the problem. Just a minor careless typo and such. Thanks again for the reply though.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by beatc View Post
    Sorry for double post but i don't see any EDIT post option. BTW, i solved the problem. Just a minor careless typo and such. Thanks again for the reply though.
    Try using the EDIT button.

    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
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    PHILIP: Newbies can't EDIT their posts. The announcement of that change is at the top of every forum page. It leads to here:

    [March 11th, 2013] Usergroup modifications

    Registered users: New members are assigned to this usergroup. This group will no longer be able to modify their posts.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Old Pedant View Post
    PHILIP: Newbies can't EDIT their posts. The announcement of that change is at the top of every forum page. It leads to here:

    [March 11th, 2013] Usergroup modifications
    Ah! So!

    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
    •