DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Basic Pattern - Javascript Interview

Example 1: Triangle Pattern - I

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

let rows = 5;
// pattern variable carries the final pattern in string format
let pattern = "";
// outer loop runs for `rows` no. of times
for (let n = 1; n <= rows; n++) {
   // inner loop runs for n
   for (let num = 1; num <= n; num++) {
      pattern += num;
   }
   // Add a new line character after contents of each line
   pattern += "\n";
}
console.log(pattern);

Enter fullscreen mode Exit fullscreen mode

Example 2: Triangle Pattern - II

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5



let rows = 5;
// pattern variable carries the final pattern in string format
let pattern = "";
// outer loop runs for `rows` no. of times
for (let n = 1; n <= rows; n++) {
   // inner loop runs for n
   for (let num = 1; num <= n; num++) {
      pattern = pattern+n;
   }
   // Add a new line character after contents of each line
   pattern += "\n";
}
console.log(pattern);

Enter fullscreen mode Exit fullscreen mode

Example 3 : Triangle Pattern - III

1
2 3
4 5 6
7 8 9 10


let rows = 4;
// variable contains the next element of the pattern
let variable = 1;
// pattern variable carries the final pattern in string format
let pattern = "";
// outer loop runs for `rows` no. of times
for (let n = 1; n <= rows; n++) {
   for (let num = 1; num <= n; num++) {
      pattern += variable+" ";
      variable++;
   }
   pattern += "\n";
}
console.log(pattern);

Enter fullscreen mode Exit fullscreen mode

Example 4: Number Pyramid Pattern - I


    1
   123
  12345
 1234567
123456789



let rows = 5;

// pattern variable carries the final pattern in string format
let pattern = "";

// outer loop runs for `rows` no. of times
for (let n = 1; n <= rows; n++) {
   // Inner Loop - I -> for the spaces
   for (let space = 1; space <= rows - n; space++) {
      pattern += " ";
   }

   // Inner Loop - II -> for the numbers
   for (let num = 1; num <= 2 * n - 1; num++) {
      pattern += num;
   }

   pattern += "\n";
}
console.log(pattern);

Enter fullscreen mode Exit fullscreen mode

Example 5: Square Star Pattern - I

*****
*****
*****
*****
*****


let rows = 5;

// pattern variable carries the final pattern in string format
let pattern = "";

// outer loop runs for `rows` no. of times
for (let n = 1; n <= rows; n++) {
   // Inner loop for printing stars
   for (let num = 1; num <= 5; num++) {
      pattern += "*";
   }
   pattern += "\n";
}
console.log(pattern);

Enter fullscreen mode Exit fullscreen mode

Example 6: Square Star Pattern - II

*****
*   *
*   *
*   *
*****


let rows = 5;

// pattern variable carries the final pattern in string format
let pattern = "";

// outer loop runs for `rows` no. of times
for (let n = 1; n <= rows; n++) {
   for (let num = 1; num <= 5; num++) {
      // print star only if it is the boundary location
      if (n == 1 || n == rows) pattern += "*";
      else {
         if (num == 1 || num == 5) {
            pattern += "*";
         } else {
            pattern += " ";
         }
      }
   }
   pattern += "\n";
}
console.log(pattern);


Enter fullscreen mode Exit fullscreen mode

Example 7 : Right Triangle Pattern

    *
   **
  ***
 ****
*****



let rows = 5;

// pattern variable carries the final pattern in string format
let pattern = "";

// outer loop runs for `rows` no. of times
for (let n = 1; n <= rows; n++) {
   // Inner loop - I -> prints spaces
   for (let space = 1; space <= rows - n; space++) {
      pattern += " ";
   }

   // Inner Loop - II -> prints stars
   for (let num = 1; num <= n; num++) {
      pattern += "*";
   }
   pattern += "\n";
}
console.log(pattern);

Enter fullscreen mode Exit fullscreen mode

Example 8 : Pyramid Triangle Pattern

    *
   ***
  *****
 *******
*********



let rows = 5;

// pattern variable carries the final pattern in string format
let pattern = "";

// outer loop runs for `rows` no. of times
for (let n = 1; n <= rows; n++) {
   // Inner Loop - I -> prints spaces
   for (let space = 1; space <= rows - n; space++) {
      pattern += " ";
   }

   // Inner Loop - II -> prints stars
   for (let num = 1; num <= 2 * n - 1; num++) {
      pattern += "*";
   }
   pattern += "\n";
}
console.log(pattern);

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
tracygjg profile image
Tracy Gilmore

Hi, Nice article.
In example 6 there is alternative approach. Instead of,

    if (num == 1 || num == 5) {
        pattern += '*';
    } else {
        pattern += ' ';
    }
Enter fullscreen mode Exit fullscreen mode

The following single line can be used,

    pattern += (num - 1) % 4 ? ' ' : '*';
Enter fullscreen mode Exit fullscreen mode

It demonstrates the use of the modulo (remainder and conditional (ternary) operators. It has the benefit of having needing one less level of indentation.

Collapse
 
zeeshanali0704 profile image
ZeeshanAli-0704

Yes, I agree with you on this.

Thanks for sharing!