pyramid pattern
JavaScript
medium
15 mins
Write a function generatePyramid(n) that returns an array of n strings representing a centered pyramid of height n using *.
- Each row is width 2n - 1 characters.
- Row i (1-indexed) contains 2i - 1 stars, centered with spaces on both sides.
- Use loops to construct the pattern.
###Examples
Input: n = 0 Output: []
Input: n = 1 Output: ["*"]
Input: n = 3 Output: [ " * ", " *** ", "*****" ]
Constraints
- n must be a finite integer >= 0.
- Return false for invalid inputs (non-integers, negatives, non-numbers, NaN, Infinity).
- Use loop-based construction (e.g., for/while).
Function Signature
function generatePyramid(n) { // Your code here }
Test Cases
- Base: n = 0, n = 1
- Small heights: n = 2, n = 3, n = 5
- Invalid inputs: -1, 1.5, null, undefined, '3', {}, NaN, Infinity
- Row properties: width = 2n - 1, star counts follow 2i - 1, only spaces and *
Companies:
tcs
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
