Basic Pattern - Javascript Interview
Tags: javascript, interview, pattern
Published: 2023-10-28 07:39:07

Example 1: Triangle Pattern - I

```js
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);

```

Example 2: Triangle Pattern - II

```js
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);

```
Example 3 : Triangle Pattern - III

```js
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);

```

Example 4: Number Pyramid Pattern - I

```js

    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);

```
Example 5: Square Star Pattern - I
```js
*****
*****
*****
*****
*****


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);

```

Example 6: Square Star Pattern - II

```js
*****
*   *
*   *
*   *
*****


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);


```

Example 7 : Right Triangle Pattern

```js
    *
   **
  ***
 ****
*****



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);

```
 Example 8 : Pyramid Triangle Pattern

```js
    *
   ***
  *****
 *******
*********



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);

```