forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mouredev#3111 from manurgdev/main
Reto mouredev#16 - TypeScript
- Loading branch information
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
Retos/Reto #16 - LA ESCALERA [Media]/typescript/manurgdev.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const createStairs = (n: number): string => { | ||
if (n === 0) return '__'; | ||
|
||
let stairs = ''; | ||
const isAscending = n > 0; | ||
const steps = Math.abs(n); | ||
let spaces = ' '.repeat(steps); | ||
|
||
stairs = isAscending ? `${spaces}_\n` : '_\n'; | ||
spaces = isAscending ? spaces.slice(0, -2) : ' '; | ||
|
||
[...Array(steps)].forEach(() => { | ||
stairs += isAscending ? `${spaces}_|\n` : `${spaces}|_\n`; | ||
spaces = isAscending ? spaces.slice(0, -2) : `${spaces} `; | ||
}); | ||
|
||
return stairs; | ||
} | ||
|
||
console.log(createStairs(6)); | ||
console.log(createStairs(0)); | ||
console.log(createStairs(-10)); |