A. Simple Pattern
1.Print a horizontal line of stars.
// *****
const horizontalLine = function (n) {
let str = ""
for (let i = 1; i <= n; i++)
str += "*";
console.log(str);
}
horizontalLine(5)
2.Print a vertical line of stars.
/*
*
*
*
*
*
*/
const verticalLine = function (n) {
let str = "";
for (let i = 1; i <= n; i++) {
str += "*\n";
}
console.log(str)
}
verticalLine(5)
B.Triangle Pattern :
1. Right Triangle Pattern & its inverted:
// *
// **
// ***
// ****
let rightTrianglePyramid = function (n) {
let str = ""
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= i; j++)
str += "*";
console.log(str);
str = "";
}
}
let rightTrianglePyramid_2 = function (n) {
let str = "";
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= i; j++)
str = "*".repeat(i);
console.log(str);
}
}
rightTrianglePyramid(7);
rightTrianglePyramid_2(7);
// 1.2 Inverted Right Triangle Pyramid Pattern:
// ****
// ***
// **
// *
let invertedrightTrianglePyramid = function (n) {
for (let i = 1; i <= n; i++) {
let str = ""
for (let j = n; j >= i; j--) {
str += "*"
}
console.log(str);
str = "";
}
}
invertedrightTrianglePyramid(7)
2.Left Triangle Pattern & its inverted:
// *
// **
// ***
// ****
let leftTrianglePyramid = function (n) {
let str = "";
for (let i = 1; i <= n; i++) {
for (let j = 1; j < n - i + 1; j++)
str += " ";
for (let k = 1; k <= i; k++)
str += "*";
console.log(str);
str = "";
}
}
leftTrianglePyramid(7)
// 2.2 Inverted Left Triangle Pattern & its inverted:
// *****
// ****
// ***
// **
// *
let invertedLeftTrianglePyramid = function (n) {
let str = "";
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= i; j++)
str += " ";
for (let k = i; k <= n; k++)
str += "*"
console.log(str);
str = "";
}
}
invertedLeftTrianglePyramid(7);
C. Pyramid Pattern & its inverted :
// *
// ***
// *****
// *******
let pyramid = function (n) {
let str = ""
for (let i = 1; i <= n; i++){
for (let j = 1; j <= n - i; j++)
str += " "
for (let k = 1; k <= (2 * i - 1); k++)
str += "*"
console.log(str);
str = "";
}
}
pyramid(7)
// Inverted Pyramid Pattern:
// *******
// *****
// ***
// *
let invertedPyramid = function (n) {
let str = "";
for (let i = 1; i <= n; i++){
for (let j = 1; j < i; j++)
str += " "
for (let k = 1; k <= (2 * (n - i + 1) - 1); k++)
str += "*"
console.log(str);
str = ""
}
}
invertedPyramid(7)
D. Diamond Pattern :
/* Diamond Pattern:
*
***
*****
*******
*****
***
*
*/
let diamond = function (semiLength) {
let n = semiLength;
let str = ""
for (let i = 1; i <= n; i++){
for (let j = 1; j < (n - i + 1); j++)
str += " "
for (let k = 1; k <= (2 * i - 1); k++)
str += "*"
console.log(str);
str = "";
}
// Lower part of pyramid
for (let i = 1; i < n; i++){
for (let j = 1; j <= i; j++)
str += " "
for (let k = 1; k <= (2 * (n - i) - 1); k++)
str += "*"
console.log(str);
str = ""
}
}
diamond(4);
E. Upcoming...
- Butterfly
ย