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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2008
    Location
    Poland
    Posts
    410
    Thanks
    155
    Thanked 2 Times in 2 Posts

    Question JS - For loop, increasing, decreasing and concatenating - help needed

    Hello, I am making some homeworks, I need to create using Javascript the following output:
    Code:
    * 2 3 4 5
    * * 3 4 5
    * * * 4 5
    * * * * 5
    * * * * *
    * * * * *
    * * * * 5
    * * * 4 5
    * * 3 4 5
    * 2 3 4 5
    The only instruction is that the constant
    Code:
    const lineLenght = 5;
    must be 5.

    This is what I created:

    Code:
    console.log("START TASK 9");
    
    //Numbers of lines
    const lineLenght = 5;
    let lineOfStarsNew4 = "";
    let wow = "";
    
    
    for (let index = 0; index < lineLenght; index++) {
        lineOfStarsNew4 = ""; // let's start the loop with an empty string
        wow = "";
    
        for (let jndex = 0; jndex <= lineLenght; jndex++) {
    
            if (jndex <= index) {
                lineOfStarsNew4 = lineOfStarsNew4 + " * "; // each time jndex increases we add a star to the variable
            }
    
           if (jndex > index + 1) {
                lineOfStarsNew4 = lineOfStarsNew4 + jndex; // to each row of starts, add the value of jndex
            }
    
        }
    
        // And now show the new value of "lineOfStarsNew4" - five times
        for (xndex = lineLenght; xndex >= lineLenght; xndex--) {
            console.log(lineOfStarsNew4);
        }
    }
    
    console.log(lineOfStarsNew4);
    
    console.log("____________ SEPARATOR____________");
    
    wow = lineOfStarsNew4;
    
    for (let index1 = 0; index1 < lineLenght; index1++) {
    
        for (let jndex1 = 5; jndex1 > 0; jndex1--) {
    
            if (jndex1 <= index1  ) {
                wow = wow.substr(4, wow.length-1);
           }
    
               if (jndex1 < index1 + 1) {
                  wow = wow + jndex1;
               }
    
        // And now show the new value of "lineOfStarsNew4" - five times
       // for (xndex1 = lineLenght; xndex1 >= lineLenght; xndex1--) {
            console.log(wow);
         //     }
    
    
    
        }
       // console.log(wow);
    
    }
    
    
    console.log("END TASK 9");


    The first part works fine. In the console shows:
    The second part, the decreasing part starts after the:
    console.log("____________ SEPARATOR____________");

    Here is the problem. I thought to decrease it using the substr method. For sure there are other easier ways, but I would like to get help in understand how I can resolve this exercise.
    Please don't give me straight away the right code to copy and paste, instead please try to explain me how what I can do to fix the problem. Please use an easy to understand language, as I am new and still studying it.


    Thanks in advance

  2. #2
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,752
    Thanks
    5
    Thanked 539 Times in 525 Posts
    NORMALLY we don't do people's homework for them on these forums (its in the rules), but since you've shown both code and effort, I'm gonna try and help as the answer is a LOT simpler than you're thinking if you can't just do a string of console.log.

    In cases like this, reducing the number of loop and letting "math be your friend" is the key.

    If we create:

    Code:
    const halfLine = Math.ceil(lineLength / 2).
    That gives you a mid-point which can be used to say how many stars should be a the start. The use of Math.ceil even makes it odd line-height friendly.

    ... assuming index runs 0..9, doing this inside the loop:

    Code:
    var count = 
      index <= halfLine ?
      index :
      lineLength + 1 - index;
    ... will give you the number of leading stars to apply. Ternary operators are your friend.

    Basically if our count is <= to the halfway point, we want to output that many starts. Otherwise the number of stars to output is our lineLength + 1 - index. For example 10 + 1 - 8 = 3... the eighth line would in fact output "* * * 4 5".

    Then you only need one outer loop from 0 to < lineLength with that count calculated inside it. Output the number of stars (0.. count) and then output (count < lineLength) numbers re-using count as your index.

    When the string is a flat repeat of the same values x number of times, you may also want to have a look at String.repeat which would let you quickly get the right number of stars into your output buffer string.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  3. #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    TX
    Posts
    3,985
    Thanks
    74
    Thanked 608 Times in 601 Posts

    Smile

    Quote Originally Posted by utnalove View Post
    Hello, I am making some homeworks, I need to create using Javascript the following output:
    Code:
    * 2 3 4 5
    * * 3 4 5
    * * * 4 5
    * * * * 5
    * * * * *
    * * * * *
    * * * * 5
    * * * 4 5
    * * 3 4 5
    * 2 3 4 5
    ...

    Code:
            if (jndex <= index) {
                lineOfStarsNew4 = lineOfStarsNew4 + " * "; // each time jndex increases we add a star to the variable
            }
    
           if (jndex > index + 1) {
                lineOfStarsNew4 = lineOfStarsNew4 + jndex; // to each row of starts, add the value of jndex
            }
    The first part works fine. In the console shows:
    The second part, the decreasing part starts after the:
    console.log("____________ SEPARATOR____________");

    ...

    Thanks in advance
    I beg to disagree, but the first loop does not appear as the desired output.

    Consider removing one of the spaces (" * ") and adding one space to the numbers output ( + index ) in the conditional statements

    Also consider how you can stop a loop before it gets to completion.

  4. #4
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    West Des Moines, Iowa
    Posts
    8,197
    Thanks
    38
    Thanked 1,092 Times in 1,088 Posts
    Here's something I'm not proud of, but it fits the bill even though I am using document.writeln()
    OK, you may not like this because 1.) I don't like to use const nor let. 2.)Don't write to the console.
    I am not proud of using document.writeln(), but don't know how far OP is with his/her coding.
    Also I see this as replacing string number with a star instead of how your doing this.
    The "The only instruction is that the constant" means I am using code to include this that I would not normally use.
    Oh Yeah: "lineLenght" S/B lineLength" for spelling.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
        <title>My Web Page</title>
    </head>
    
    <body>
        
    <script>
    var lineLength = 5;
    var lineOfStarsNew4 = "";
    var lineOfStars = "";
    var wow = "";
    var index = 1;
    var place = document.getElementById('jeramy');
    /* This loop to construct the initial line of numbers */
    /* I would just set lineOfStarsNew4 = '12345' */
    /* and forget the var lineLength = 5; */
    for (index = 1; index <= lineLength; index++) {
        lineOfStarsNew4 += index; 
    }
    /* This writes the first part */
    for (index = 1; index <= lineLength; index++) {
        lineOfStars += '*';
        wow = lineOfStars.concat(lineOfStarsNew4.substring(index));
        document.writeln(wow+'<br>');
    }
    /* This writes the last part */
    lineOfStars = '******';
    for (index = lineLength; index >= 1; index--) {
        lineOfStars = lineOfStars.replace('*', '');
        wow = lineOfStars.concat(lineOfStarsNew4.substring(index));
        document.writeln(wow+'<br>');
    }
    </script>
    </body>
    </html>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  5. #5
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,752
    Thanks
    5
    Thanked 539 Times in 525 Posts
    Quote Originally Posted by sunfighter View Post
    Here's something I'm not proud of
    Op asked for us NOT to give complete code so they could try to figure it out on their own since its homework. I was worried MY post had too much code.

    That said, yeah.. don't be proud of that (I kid, I kid... not kidding.)
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  6. #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Location
    Here
    Posts
    4,011
    Thanks
    58
    Thanked 712 Times in 707 Posts
    Is it OK to post complete solutions as long as they come with the warning that they shouldn't be used unless you want to get busted for plagiarism?

    PHP Code:
    const lineLength 5;

    for (var 
    0lineLength 2i++) {
        if (
    lineLength) {
            var 
    tmp = Array(1).fill("*").concat(Array(lineLength 1).fill().map((ej) => 2));
        } else {
            var 
    tmp = Array(lineLength i).fill("*").concat(Array(lineLength).fill().map((ej) => lineLength j).reverse());
        }
        
    console.log(tmp.join(""));



 

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
  •