DEV Community

Cover image for Create 12 alphabet patterns in javascript
Satish Chandra Gupta
Satish Chandra Gupta

Posted on • Edited on • Originally published at tutorialstonight.com

Create 12 alphabet patterns in javascript

Practice makes a man perfect!
Let's create a few interesting alphabet patterns in javascript and understand loop in a better way.

What is alphabet patterns?

Alphabet patterns are a series of alphabets (Uppercase or Lowercase) which create some shape or geometrical figure.
Creating alphabet patterns or star patterns helps us to understand how to use and control loops.
Pattern questions are also asked in a freshers programming interview.
For explanation with real-time running code, check the article.

Pattern 1:

A
A B
A B C
A B C D
A B C D E

let n = 5; // you can take input from prompt or change the value
let string = "";
// External loop
for (let i = 1; i <= n; i++) {
  // printing characters
  for (let j = 0; j < i; j++) {
    string += String.fromCharCode(j + 65);
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 2:

A
B B
C C C
D D D D
E E E E E

let n = 5; // you can take input from prompt or change the value
let string = "";
// External loop
for (let i = 1; i <= n; i++) {
  // printing characters
  for (let j = 0; j < i; j++) {
    string += String.fromCharCode((i - 1) + 65);
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 3:

A
B C
D E F
G H I J
K L M N O

let n = 5;
let string = "";
let count = 0;
// External loop
for (let i = 1; i <= n; i++) {
  for (let j = 0; j < i; j++) {
    string += String.fromCharCode(count + 65);
    count++; // increment cause next alphabet
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 4:

ABCDE
ABCD
ABC
AB
A

let n = 5;
let string = "";
// External loop
for (let i = 1; i <= n; i++) {
  for (let j = 0; j < n - i + 1; j++) {
    string += String.fromCharCode(j + 65);
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 5:

EDCBA
EDCB
EDC
ED
E

let n = 5;
let string = "";
// External loop
for (let i = 1; i <= n; i++) {
  for (let j = 0; j < n - i + 1; j++) {
    string += String.fromCharCode((n - 1 - j) + 65);
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 6:

EDCBA
DCBA
CBA
BA
A

let n = 5;
let string = "";
// External loop
for (let i = 1; i <= n; i++) {
  for (let j = 0; j < n - i + 1; j++) {
    string += String.fromCharCode((n - i - j) + 65);
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 7:

A
ABC
ABCDE
ABCDEFG
ABCDEFGHI

let n = 5;
let string = "";
// External loop
for (let i = 1; i <= n; i++) {
  // creating spaces
  for (let j = 0; j < n - i; j++) {
    string += " ";
  }
  // creating alphabets
  for (let k = 0; k < 2 * i - 1; k++) {
    string += String.fromCharCode(k + 65);
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 8:

A
BCD
EFGHI
JKLMNOP
QRSTUVWXY

let n = 5;
let string = "";
let count = 0;
// External loop
for (let i = 1; i <= n; i++) {
  // creating spaces
  for (let j = 0; j < n - i; j++) {
    string += " ";
  }
  // creating alphabets
  for (let k = 0; k < 2 * i - 1; k++) {
    string += String.fromCharCode(count + 65);
    count++;
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 9:

ABCDEFGHI
ABCDEFG
ABCDE
ABC
A

let n = 5;
let string = "";
// External loop
for (let i = 1; i <= n; i++) {
  // creating spaces
  for (let j = 1; j < i; j++) {
    string += " ";
  }
  // creating alphabets
  for (let k = 0; k < 2 * (n - i + 1) - 1; k++) {
    string += String.fromCharCode(k + 65);
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 10:

A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A

let n = 5;
let string = "";
// Pyramid
for (let i = 1; i <= n; i++) {
  for (let j = 1; j < n - i + 1; j++) {
    string += " ";
  }
  for (let k = 0; k < 2 * i - 1; k++) {
    string += String.fromCharCode(k + 65);
  }
  string += "\n";
}
// Reverse Pyramid
for (let i = 1; i <= n - 1; i++) {
  for (let j = 1; j < i + 1; j++) {
    string += " ";
  }
  for (let k = 0; k < 2 * (n - i) - 1; k++) {
    string += String.fromCharCode(k + 65);
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 11:

ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI

let n = 5;
let string = "";
// Reverse Pyramid
for (let i = 1; i <= n; i++) {
  for (let j = 1; j < i; j++) {
    string += " ";
  }
  for (let k = 0; k < 2 * (n - i + 1) - 1; k++) {
    string += String.fromCharCode(k + 65);
  }
  string += "\n";
}
// Pyramid
for (let i = 1; i <= n - 1; i++) {
  for (let j = 1; j < n - i; j++) {
    string += " ";
  }
  for (let k = 0; k < 2 * (i + 1) - 1; k++) {
    string += String.fromCharCode(k + 65);
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Pattern 12:

A
AB
ABC
ABCD
ABCDE
ABCD
ABC
AB
A

let n = 5;
let string = "";
// Pyramid
for (let i = 1; i <= n; i++) {
  for (let k = 0; k < i; k++) {
    string += String.fromCharCode(k + 65);
  }
  string += "\n";
}
// Reverse Pyramid
for (let i = 1; i <= n - 1; i++) {
  for (let k = 0; k < n - i; k++) {
    string += String.fromCharCode(k + 65);
  }
  string += "\n";
}
console.log(string);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)