Today, as part of my Namaste JavaScript learning journey (Day 12), I explored and solved some classic pattern problems in JavaScript. These are simple yet powerful exercises that help build logical thinking and comfort with loops.
✅ Topics Covered:
- Nested Loops in JavaScript
- Printing patterns with * and numbers
- Loop logic and formatting output
📌 Problem 1: Print the Star Square
Print a square pattern made of *
, where the number of rows and columns are provided as input.
💡 Approach:
We use a nested loop:
- Outer loop runs for each row.
- Inner loop runs for each column, appending a * to the output string.
- After each row, we add a newline \n.
📥 Input:
Number_Of_Row = 4, Number_Of_Col = 4
📥 Output:
* * * *
* * * *
* * * *
* * * *
🧪 Solution:
console.log('+++++++++ Print the Star Square +++++++++');
function Print_Star_Square(Number_Of_Row, Number_Of_Col) {
let Star = "";
for (let i = 0; i < Number_Of_Row; i++) {
for (let j = 0; j < Number_Of_Col; j++) {
Star += '* ';
}
Star += '\n'; // Move to the next line after each row
}
return Star;
}
console.log(Print_Star_Square(4, 4));
console.log('++++++++++++++++++++++++++++++++++++++++++');
📌 Problem 2: Right-Angled Star Triangle
Print a right-angled triangle of stars with n rows.
💡 Approach:
- Each row prints one more * than the previous one, starting from 1
- The Outer Loop is Responsive for Row and the Inner Low Responsible for Column.
📥 Input:
n = 4
📥 Output:
*
* *
* * *
* * * *
🧪 Solution:
function Print_Right_Angled_Star_Triangle(n) {
let Star = "";
for (let i = 0; i < n; i++) {
for (let j = 0; j <= i; j++) {
Star += '* ';
}
Star += '\n';
}
return Star;
}
console.log(Print_Right_Angled_Star_Triangle(4));
📌 Problem 3: Right-Angled Number Triangle
Print a triangle where each row contains increasing numbers from 1 to the row number.
💡 Approach:
For each row i, we loop from 1 to i+1, adding numbers incrementally.
📥 Input:
n = 4
📥 Output:
1
12
123
1234
🧪 Solution:
function Print_Right_Angled_Star_Number_Triangle(n) {
let Number = "";
for (let i = 0; i < n; i++) {
for (let j = 0; j <= i; j++) {
Number += j + 1;
}
Number += "\n";
}
return Number;
}
console.log(Print_Right_Angled_Star_Number_Triangle(4));
///
📌 Problem 4: Right-Angled Triangle of Repeated Numbers
Print a triangle where each row contains repeated numbers equal to the row number.
💡 Approach:
For each row i
, print (i+1)
repeatedly i+1
times.
📥 Input:
n = 4
📥 Output:
1
22
333
4444
🧪 Solution:
function Right_Angled_Triangle_of_Repeated_Numbers(n) {
let Number = "";
for (let i = 0; i < n; i++) {
for (let j = 0; j <= i; j++) {
Number += (i + 1);
}
Number += "\n";
}
return Number;
}
console.log(Right_Angled_Triangle_of_Repeated_Numbers(4));
🧠 Key Concepts Practiced:
- Nested for loops
- String building and formatting output
- Pattern logic using rows and columns
- Console logging for step-by-step output
🚀 What’s Next?
I’ll continue with more pattern problems, string manipulation, and functions in upcoming days. These exercises are helping me build a solid base for real-world JavaScript development.
📝 Follow me for more JavaScript learning logs!
💬 Let me know in the comments: Which pattern did you find most interesting?
Top comments (0)