From eba1780db245d074e7f2b72cd9c4ab32443e8752 Mon Sep 17 00:00:00 2001 From: Billy Torres Date: Mon, 8 Jan 2024 09:10:50 -0600 Subject: [PATCH] Reto #16 - JavaScript --- .../javascript/BRivasTorres.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Retos/Reto #16 - LA ESCALERA [Media]/javascript/BRivasTorres.js diff --git a/Retos/Reto #16 - LA ESCALERA [Media]/javascript/BRivasTorres.js b/Retos/Reto #16 - LA ESCALERA [Media]/javascript/BRivasTorres.js new file mode 100644 index 0000000000..6e0c1869d8 --- /dev/null +++ b/Retos/Reto #16 - LA ESCALERA [Media]/javascript/BRivasTorres.js @@ -0,0 +1,38 @@ +const createLadder = (n) => { + if(n === 0) return "__" + + let steps = "_|" + let res = "" + let spaces = "" + + if(n > 0) { + let i = 0 + let temp = "" + while(n !== 0) { + temp += steps[i] + i === 1 ? temp += "\n" + spaces : null + i === 1 ? (i = 0) : i++; + spaces += " "; + n-- + } + res = temp.split(" ").reverse().join(" ") + } else { + spaces += " " + let temp = "" + let i = 0; + while (n !== 0) { + temp += steps[i]; + i === 0 ? (temp += "\n" + spaces) : null; + i === 1 ? (i = 0) : i++; + spaces += " "; + n++; + } + res = temp + } + return res +} + +console.log(createLadder(-3)) +console.log(createLadder(-7)) +console.log(createLadder(10)) +console.log(createLadder(18)) \ No newline at end of file