π Problem 1: Generate Alternating Triangle
Write a function that takes a number n
and prints a triangle of size n
where each row contains alternating 1s
and 0s
, starting with 1
.
π‘ Approach:
To generate this alternating binary triangle, follow these steps:
-
Create two variables:
-
pattern
β an empty string to accumulate the full result. -
currentValue
β a temporary variable initialized inside each row to1
.
-
-
Outer loop (
i
from0
ton - 1
):- Controls the number of rows.
-
For each row:
- Set
currentValue = 1
before entering the inner loop.
- Set
-
Inner loop (
j
from0
toi
):- Controls the number of columns (or characters) in that row.
- Append
currentValue
topattern
. - Flip
currentValue
:- If
currentValue === 1
, set it to0
. - Else, set it to
1
.
- If
π₯ Input:
n = 5
π₯ Output:
1
10
101
1010
10101
π§ͺ Solution:
/**
* Generates a triangle pattern with alternating 1s and 0s
* @param {number} n - Number of rows in the pattern
* @returns {string} - The generated triangle pattern
*/
function generateAlternatingTriangle(n) {
let pattern = "";
for (let i = 0; i < n; i++) {
let currentValue = 1; // Start each row with 1
for (let j = 0; j <= i; j++) {
pattern += currentValue;
currentValue = currentValue === 1 ? 0 : 1; // Toggle value
}
pattern += "\n"; // New line after each row
}
return pattern;
}
// β
Test Case
const n = 5;
console.log(`Pattern for n = ${n}:\n`);
console.log(generateAlternatingTriangle(n));
//
π Problem 2: Generate Hollow Square Pattern
Write a function that takes a number n and prints a hollow square of size n Γ n
using asterisks .
The border should be filled with ``, and the inside should be filled with spaces.
π‘ Approach:
To generate this hollow square pattern, follow these steps:
-
Create a variable:
-
pattern
β an empty string to accumulate the final result.
-
-
Outer loop (i from 0 to n - 1):
- Represents each row of the square.
-
Inner loop (j from 0 to n - 1):
- Represents each column in the current row.
- Append
*
topattern
if:- Itβs the first or last row β
i === 0 || i === n - 1
- Itβs the first or last column β
j === 0 || j === n - 1
- Itβs the first or last row β
- Otherwise, append a space ( ) for the hollow center.
After each row, append a newline character
\n
to move to the next line.
π₯ Input:
n = 10
π₯ Output:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
π§ͺ Solution:
/**
* Generates a hollow square pattern of size n x n
* @param {number} n - Size of the square
* @returns {string} - The generated square pattern
*/
function generateHollowSquare(n) {
let pattern = "";
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (i === 0 || i === n - 1 || j === 0 || j === n - 1) {
pattern += "*";
} else {
pattern += " ";
}
}
pattern += "\n"; // New line after each row
}
return pattern;
}
// β
Test Case
const n = 10;
console.log(`Pattern for n = ${n}:\n`);
console.log(generateHollowSquare(n));
π Problem 3: Generate Diamond Pattern
Write a function that takes an odd number n
and prints a diamond shape using asterisks (*)
.
The diamond should be symmetrical, with the widest point in the middle.
π‘ Approach:
To generate this diamond pattern, follow these steps:
Create a variable:
pattern
β an empty string to store the final result.-
First Part β Top Half of the Diamond (including the middle line):
- Loop
i
from1
ton
, incrementing by 2. - For each row:
- Add spaces:
(n - i) / 2
spaces for centering. - Add
i
asterisks. - Add a newline
\n
.
- Add spaces:
- Loop
-
Second Part β Bottom Half of the Diamond:
- Loop
i
fromn - 2
to1
, decrementing by 2. - For each row:
- Add spaces:
(n - i) / 2
spaces. - Add
i
asterisks. - Add a newline
\n
.
- Add spaces:
- Loop
Inside Loop 1 And Loop 2
π₯ Input:
n = 7;
π₯ Output:
*
***
*****
*******
*****
***
*
π§ͺ Solution:
/**
* Generates a diamond pattern of stars for a given odd number n
* @param {number} n - Must be an odd number representing the height
* @returns {string} - The generated diamond pattern
*/
function generateDiamond(n) {
let pattern = "";
// First Part: Increasing stars
for (let i = 1; i <= n; i += 2) {
for (let s = 0; s < (n - i) / 2; s++) {
pattern += " ";
}
for (let b = 0; b < i; b++) {
pattern += "*";
}
pattern += "\n";
}
// Second Part: Decreasing stars
for (let i = n - 2; i >= 1; i -= 2) {
for (let s = 0; s < (n - i) / 2; s++) {
pattern += " ";
}
for (let b = 0; b < i; b++) {
pattern += "*";
}
pattern += "\n";
}
return pattern;
}
// β
Test Case
const n = 7;
console.log(`Diamond pattern for n = ${n}:\n`);
console.log(generateDiamond(n));
Top comments (0)