dcsimg
www.webdeveloper.com
Results 1 to 9 of 9

Thread: Can i write this javaScript code easier please?

  1. #1
    Join Date
    May 2017
    Posts
    4

    Can i write this javaScript code easier please?

    Hi everyone and thanks for this forum and opportunity to ask a question and inmprove the knowledge. My question is simple. I started with javascript one week ago and I would like to ask if this JavaScript code can be written simpler? Thanks

    It does only the row of numbers from one to 40.

    <script>
    var number = 0;
    var endnumber = 39;

    function first(){
    if (number <= endnumber) {
    number++;
    document.write(number + "<br />")

    second();
    }else{
    document.write("This is all!");
    }
    }
    function second(){
    if (number <= endnumber) {
    number++;
    document.write(number + "<br />")

    first();
    }else{
    document.write("This is all!");
    }
    }

    first();

    </script>

  2. #2
    Join Date
    May 2017
    Posts
    4
    I'm afraid the question is not clear enough. But I think loops could help you:
    Code:
    for(var i = 0; i < 40; i++) {
        //This part will run 40 times, once per number, as in, 1, 2, 3, 4 ... 40
    }
    If it's not, then try rephrasing.

  3. #3
    Join Date
    May 2017
    Posts
    4
    Thanks erandros! Thats exactly what I meant.

    Code:
    for(var i = 1; i <= 40; i++){
              document.write(i + "<br />");
            }
    It is much simpler than the first code. However when it starts to write in document it starts with the symbol "<". Can I get rid of it somehow?

    Now the result in browser looks like:

    <1
    2
    3
    4
    .
    .

    cheers

  4. #4
    Join Date
    May 2017
    Posts
    4
    Sorry its my bad. I forget one "<" at the top of my document. Yeah guys you can laugh.

  5. #5
    Join Date
    May 2017
    Posts
    4
    Glad I could help! I'll try to find a programming basics tutorial on the week and PM it to you.
    It might make your programming easier.

  6. #6
    Join Date
    May 2017
    Posts
    4
    Thanks. :-) iam currently learning from Beginner JavaScript Tutorial from thenewboston channel. He has nice short videos and explain the basics in the way which is not boring and is easy to understand.
    Anyways I will appreciate any tutorial which u PM to me.

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,735
    I would suggest that you consult online sites like W3Schools and when you're more proficient, the MDN website is a good resource to use. Both give examples of how to use the various functions.

    Then you have this site as a resource to help you understand code errors as well as learning by others mistakes and how the problems were resolved also exist.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  8. #8
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,816
    Hi there VladimirM,

    bearing in mind that you really should not be using
    "document.write()", this is short and to the point...

    Code:
    
    <script>
    (function() {
       'use strict';
       var number = 0;
       var endnumber = 39;
    while( number <= endnumber ) {
       number ++;
       document.write( number + '<br>' );
     }
       document.write( 'This is all!' ); 
    }());
    </script>

    coothead
    ~ the original bald headed old fart ~

  9. #9
    Join Date
    May 2017
    Posts
    4
    you know, you can add my discord erandros#1829 if you have questions I can help

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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



Recent Articles