From f0bff99aa488f6822ec37a116afb355d2998120a Mon Sep 17 00:00:00 2001 From: Vasily Guzov Date: Thu, 3 Oct 2024 12:55:37 +0300 Subject: [PATCH] [6kyu][string] towerBuilder https://www.codewars.com/kata/576757b1df89ecf5bd00073b Build a pyramid-shaped tower, as an array/list of strings, given a positive integer number of floors. A tower block is represented with "*" character.For example, a tower with 3 floors looks like this: --- lib/towerBuilder.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 lib/towerBuilder.ts diff --git a/lib/towerBuilder.ts b/lib/towerBuilder.ts new file mode 100644 index 0000000..8098b32 --- /dev/null +++ b/lib/towerBuilder.ts @@ -0,0 +1,17 @@ +export const towerBuilder = (nFloors: number): string[] => { + + const floors: string[] = []; + + for (let n = 1; n < nFloors + 1; n++) { + const stars = "*".repeat(n * 2 - 1); + const space = ' '.repeat(nFloors - n); + const res = space + stars + space; + floors.push(res); + } + + return floors; + +} + +console.log(towerBuilder(3)) +console.log(towerBuilder(6))