verticalSlashes - Interview question of the week from rendezvous with cassidoo
I do enjoy getting Cassidy’s newsletter late Sunday/early Monday and trying my hand at the “interview [questions] of the week.”
Interview question of the week
This week’s question: Write a function that takes a string of slashes (
\
and/
) and returns all of those slashes drawn downwards in a line connecting them.Example:
$ verticalSlashes('\\\//\/\\') \ \ \ / / \ / \ \ $ verticalSlashes('\\\\') \ \ \ \
My solution
function verticalSlashes(str) {
let minIndent = 0;
let lastIndent = 0;
const lines = [];
for (let i = 0; i < str.length; i++) {
const c = str[i];
if (c === "\\") {
lastIndent += 1;
}
lines.push({
c,
relativeIndent: lastIndent,
});
if (c === "/") {
lastIndent -= 1;
}
minIndent = Math.min(minIndent, lastIndent);
}
const baseIndent = Math.abs(minIndent);
return lines
.map(
({ c, relativeIndent }) =>
`${" ".repeat(baseIndent + relativeIndent - 1)}${c}`
)
.join("\n");
}
// Tests
console.log(verticalSlashes("\\\\\\//\\/\\\\"));
console.log(verticalSlashes("\\\\\\\\"));
// Extra tests
console.log(verticalSlashes("////"));
console.log(verticalSlashes("/\\\\\\\\"));
console.log(verticalSlashes("\\\\\\|Hello World|///"));