Facebook Pixel

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 🔥

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.