Skip to content

Instantly share code, notes, and snippets.

@vvgomes
Last active February 25, 2022 15:52
Show Gist options
  • Save vvgomes/ac27c8b2d5a0937c3228701d3f336f11 to your computer and use it in GitHub Desktop.
Save vvgomes/ac27c8b2d5a0937c3228701d3f336f11 to your computer and use it in GitHub Desktop.
/*
height = 6
.....∆
....∆∆∆
...∆∆∆∆∆
..∆∆∆∆∆∆∆
.∆∆∆∆∆∆∆∆∆
∆∆∆∆∆∆∆∆∆∆∆
*/
function buildPyramid(height, layerCount = 1, pyramid = []) {
if (layerCount > height) return pyramid.join("\n");
const padCount = height + layerCount - 1;
const brickCount = layerCount * 2 - 1;
const layer =
Array(brickCount)
.fill("∆", 0, brickCount)
.join("")
.padStart(padCount, " ");
return buildPyramid(height, layerCount + 1, pyramid.concat(layer));
}
console.log(buildPyramid(12));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment