Created
November 22, 2022 16:36
-
-
Save areinmeyer/fab5f10cf47f95d07c67db07db20955b to your computer and use it in GitHub Desktop.
Vertical Slashes solution in node
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
#!/usr/bin/env node | |
const input = process.argv.slice(2); | |
const slashes = String.raw`${input[0]}`; | |
let output = ""; | |
const verticalSlashes = (count, input) => { | |
if (input.length === 0) return ""; | |
const currSlash = input[0] | |
switch (currSlash) { | |
case "/": | |
newCount = count === 0 ? 0 : count - 1 | |
output = genOutput(newCount, currSlash) | |
return output + verticalSlashes(newCount, input.slice(1, input.length)); | |
case "\\": | |
output = genOutput(count, currSlash) | |
return output + verticalSlashes(count + 1, input.slice(1, input.length)); | |
default: | |
output = genOutput(count, currSlash) | |
return output + verticalSlashes(count + 1, input.slice(1, input.length)); | |
} | |
}; | |
const genOutput = (count, slash) => { | |
return " ".repeat(count) + slash + "\n"; | |
} | |
console.log(verticalSlashes(0, slashes)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment