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.
- Loading branch information
1 parent
8b3d8c5
commit 2407de7
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
Retos/Reto #16 - LA ESCALERA [Media]/typescript/gefermanpernia.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,49 @@ | ||
/* | ||
* Crea una función que dibuje una escalera según su número de escalones. | ||
* - Si el número es positivo, será ascendente de izquiera a derecha. | ||
* - Si el número es negativo, será descendente de izquiera a derecha. | ||
* - Si el número es cero, se dibujarán dos guiones bajos (__). | ||
* | ||
* Ejemplo: 4 | ||
* _ | ||
* _| | ||
* _| | ||
* _| | ||
* _| | ||
* | ||
*/ | ||
|
||
|
||
function dibujarEscalera(numeroEscalones: number): void { | ||
const isAscendente = numeroEscalones > 0; | ||
const numeroPasos = Math.abs(numeroEscalones); | ||
|
||
if (numeroPasos === 0) { | ||
console.log('__'); | ||
return; | ||
} | ||
|
||
|
||
let escalera = ''; | ||
|
||
if (!isAscendente) escalera = "_\n"; | ||
else escalera= " ".repeat(numeroPasos)+" _\n" | ||
|
||
for (let i = 0; i < numeroPasos; i++) { | ||
const espacios = ' '.repeat(isAscendente ? numeroPasos - i : i); | ||
const peldaño = isAscendente ? `${espacios}_|` : `${espacios} |_`; | ||
escalera += `${peldaño}\n`; | ||
|
||
} | ||
|
||
|
||
|
||
console.log(escalera); | ||
} | ||
|
||
|
||
dibujarEscalera(-10) | ||
|
||
dibujarEscalera(0) | ||
|
||
dibujarEscalera(10) |