The main motive of learning programming is to gain the ability to instruct computers to perform tasks and solve problems.It can be anything building some software that sell something, creating games, personal portfolio's, blog website like DEV.to and lot's and lot's more.
But for reaching that far the first step of each part is having ability of Logical and Analytical Reasoning.Pattern-based questions develop the logical and analytical reasoning skills. They often involve identifying and analysing the structure of a pattern, and then using that understanding to generate a solution. This helps in developing problem-solving skills, which are essential in DSA.
So today In this part I am sharing 11 patterns that a beginner can start's in JavaScript. After that in second part I will share some more complex pattern problems and solutions.
// Pattern : 1
// *****
// *****
// *****
// *****
// *****
// This function will print the Pattern 1 that is described above
const printSquare = (input) => {
let patternCapturer = "";
for (let i = 1; i <= input; i++) {
for (let j = 1; j <= input; j++) {
patternCapturer += "* ";
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
printSquare(5);
// Pattern 2 :
// *
// **
// ***
// ****
// *****
// This function will print Pattern 2 as described above
const printRightAngledTriangle = (input) => {
let patternCapturer = "";
for (let i = 1; i <= input; i++) {
for (let j = 1; j <= i; j++) {
patternCapturer += "* ";
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
printRightAngledTriangle(5);
// Pattern: 3
// 1
// 12
// 123
// 1234
// 12345
// This function will print Pattern 2 as described above
const printRightAngledTriangleWithNums = (input) => {
let patternCapturer = "";
for (let i = 1; i <= input; i++) {
for (let j = 1; j <= i; j++) {
patternCapturer += `${j} `;
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
printRightAngledTriangleWithNums(5);
// Pattern: 4
// 1
// 22
// 333
// 4444
// 55555
// This function will print Pattern 2 as described above
const printRightAngledTriangleWithIdenticalNums = (input) => {
let patternCapturer = "";
for (let i = 1; i <= input; i++) {
for (let j = 1; j <= i; j++) {
patternCapturer += `${i} `;
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
printRightAngledTriangleWithIdenticalNums(5);
// Pattern : 5
// *****
// ****
// ***
// **
// *
// This function will print Pattern 5 as described above
const reversedRtAngledT = (input) => {
let patternCapturer = "";
for (let i = 1; i <= input; i++) {
for (let j = input; j >= i; j--) {
patternCapturer += "* ";
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
reversedRtAngledT(5);
// Pattern : 6
// 12345
// 1234
// 123
// 12
// 1
// This function will print Pattern 6 as described above
const reversedRtAngledTWithNums = (input) => {
let patternCapturer = "";
for (let i = 1; i <= input; i++) {
for (let j = input; j >= i; j--) {
patternCapturer += `${input - (j - 1)}`;
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
reversedRtAngledTWithNums(5);
// Pattern : 7
// *
// ***
// *****
// This function will print Pattern 7 as described above
const pyramidPattern = (input) => {
let patternCapturer = "";
for (let i = 1; i <= input; i++) {
// Printing Spaces
for (let j = 1; j <= input - i; j++) {
patternCapturer += " ";
}
//Printing Stars
for (let k = 0; k < 2 * i - 1; k++) {
patternCapturer += "*";
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
pyramidPattern(5);
// Pattern :8
// *****
// ***
// *
// This function will print Pattern 8 as described above
const reversePyramid = (input) => {
let patternCapturer = "";
for (let i = 0; i < input; i++) {
// For printing spaces
for (let j = 0; j < i; j++) {
patternCapturer += " ";
}
// For printing stars
for (let k = 0; k < 2 * (input - i) - 1; k++) {
patternCapturer += "*";
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
reversePyramid(5);
// Pattern : 9
// *
// ***
// *****
// *****
// ***
// *
// This function will print Pattern 9 as described above
const printDiamond = (input) => {
let patternCapturer = "";
for (let i = 1; i <= input; i++) {
// for spaces
for (let j = 1; j <= input - i; j++) {
patternCapturer += " ";
}
//for Stars
for (let k = 0; k < 2 * i - 1; k++) {
patternCapturer += "*";
}
patternCapturer += "\n";
}
for (let i = 0; i < input; i++) {
// for spaces
for (let j = 0; j < i; j++) {
patternCapturer += " ";
}
// for stars
for (let k = 0; k < 2 * (input - i) - 1; k++) {
patternCapturer += "*";
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
printDiamond(5);
// Pattern: 10
// 1
// 01
// 101
// 0101
// 10101
// This function will print Pattern 10 as described above
const printRightAngledTriangleWithBoolNums = (input) => {
let patternCapturer = "";
for (let i = 1; i <= input; i++) {
for (let j = 1; j <= i; j++) {
patternCapturer += `${(i + j) % 2 == 0 ? 1 : 0} `;
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
printRightAngledTriangleWithBoolNums(5);
// Pattern 11
// *
// * *
// * * *
// * * * *
// * * * * *
// * * * *
// * * *
// * *
// *
// This function will print Pattern 11 as described above
const rightArrowPattern = (input) => {
let patternCapturer = "";
for (let i = 1; i <= input; i++) {
for (let j = 1; j <= i; j++) {
patternCapturer += "* ";
}
patternCapturer += "\n";
}
for (let i = 1; i < input; i++) {
for (let j = input - 1; j >= i; j--) {
patternCapturer += "* ";
}
patternCapturer += "\n";
}
console.log(patternCapturer);
};
// Providing Input value on the basis of this Pattern Printed
rightArrowPattern(5);
GITHUB LINK : https://github.com/Jagroop2000/DSA-One-Javascript/blob/master/patterns/patterns.js
Top comments (5)
Wow, that must have taken you quiet a while to write π thank you!!
On the first and second patterns you could also use array methods. But I think they would get way more complex and worse as you go through the more complex patterns. But here are more solutions for 1 and 2:
Hi @samuel-braun !!
Thanks for sharing. It's good to see that we can write it in just 1 line but my main motive of posting pattern based question is developing logical thinking. Like for begineers , it would help them for writing pseudocode and better understanding of using loops.
No doubt while development we would prefer inbuild functions so that number of lines of code would be lesser and easy to understand.
Thanks for sharing !! I really need this.
Very usefull !!
I am happy that you found this useful !!