Monday, 21 December 2015

Kata Day 25: Ghostbusters


Problem Description: Oh no! Ghosts have reportedly swarmed the city. It's your job to get rid of them and save the day!

In this kata, strings represent buildings while whitespaces within those strings represent ghosts.

So what are you waiting for? Return the building(string) without any ghosts(whitespaces)!

Example:

ghostBusters("Sky scra per");
Should return:

"Skyscraper"
If the building contains no ghosts, return the string:

"You just wanted my autograph didn't you?"

Level:7kyu

Link to Kata: Ghostbusters 

In this Kata we have to remove the white spaces from the given string. We have to keep a check if the given string contains white spaces or not. If it doesn't then we return a string as reply - "You just wanted my autograph didn't you?"

We start by checking the index of white space in the given string by using Javascript's .indexOf() function. If it is equal to -1 then we return our reply otherwise we use replace function of regex and replace all the white spaces with no spaces.

In the end we return our string from which we have removed the white spaces.

Here is a link to the code: Kata Day 25

Please mention your suggestions or your doubts in the comment below.


Happy coding. :)

No comments:

Post a Comment