Skip to content

Instantly share code, notes, and snippets.

@vagnercardosoweb
Last active May 6, 2023 18:52
Show Gist options
  • Select an option

  • Save vagnercardosoweb/e3fea26277081fcc6bc45adf22efd10f to your computer and use it in GitHub Desktop.

Select an option

Save vagnercardosoweb/e3fea26277081fcc6bc45adf22efd10f to your computer and use it in GitHub Desktop.
Test result that happened on the Web Summit Rio da Codewars and Andela
const makeStaircase = (value) => {
const items = value.split(" ").filter(Boolean);
if (items.length === 0) return value;
let index = 0;
let result = "";
let spaceSize = 0;
for (let item of items) {
let space = " ".repeat(spaceSize);
if (index % 2 === 1) {
item = space.concat(item.split("").join("\n".concat(space)));
} else {
spaceSize += item.length - 1;
item = space.concat(item);
}
if (index != items.length - 1) {
item = item.concat("\n");
}
result += item;
index++;
}
return result.trim();
};
module.exports = makeStaircase;
const makeStaircase = require("./challenge");
describe("makeStaircase", () => {
it("should work on an empty string", () => {
expect(makeStaircase("")).toBe("");
});
it("should work on 'Rio'", () => {
expect(makeStaircase("Rio")).toBe("Rio");
});
it("should work on 'Web Summit Rio'", () => {
expect(makeStaircase("Web Summit Rio")).toBe(
`
Web
S
u
m
m
i
t
Rio
`.slice(1, -1)
);
});
it("should work on 'Welcome to the Andela booth!'", () => {
expect(makeStaircase("Welcome to the Andela booth!")).toBe(
`
Welcome
t
o
the
A
n
d
e
l
a
booth!
`.slice(1, -1)
);
});
it("should work on 'a lot of tiny little words in a sentence'", () => {
expect(makeStaircase("a lot of tiny little words in a sentence")).toBe(
`
a
l
o
t
of
t
i
n
y
little
w
o
r
d
s
in
a
sentence
`.slice(1, -1)
);
});
it("should work on 'Thanks for checking out the Andela booth!'", () => {
expect(makeStaircase("Thanks for checking out the Andela booth!")).toBe(
`
Thanks
f
o
r
checking
o
u
t
the
A
n
d
e
l
a
booth!
`.slice(1, -1)
);
});
});
{
"devDependencies": {
"jest": "^29.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment