🚀 Check out my $1,000,000 journey!

JavaScript Coding Challenge: "Dubstep"

4 min read

JCC Dubstep

A coding challenge from Codewars.

Problem description

Polycarpus works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them.

Let’s assume that a song consists of some number of words (that don’t contain WUB). To make the dubstep remix of this song, Polycarpus inserts a certain number of words “WUB” before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including “WUB”, in one string and plays the song at the club.

For example, a song with words “I AM X” can transform into a dubstep remix as “WUBWUBIWUBAMWUBWUBX” and cannot transform into “WUBWUBIAMWUBX”.

Recently, Jonny has heard Polycarpus’s new dubstep track, but since he isn’t into modern music, he decided to find out what was the initial song that Polycarpus remixed. Help Jonny restore the original song.

Input

The input consists of a single non-empty string, consisting only of uppercase English letters, the string’s length doesn’t exceed 200 characters

Output

Return the words of the initial song that Polycarpus used to make a dubsteb remix. Separate the words with a space.

Example: songDecoder("WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB") will return: WE ARE THE CHAMPIONS MY FRIEND

There is a lot of text to explain the problem, but the concept is pretty easy. Remove the 'WUB' - set of characters from the given string and make sure to keep the spaces between the remaining words.

Solution one - replace + ReGeX

This solution is in 3 steps:

  1. Replace all he 'WUB' set of characters with an empty string (' ') using ReGeX (read more about it here)
  2. Remove the extra spaces between words using ReGeX, again
  3. Trim the remaining whitespace from both sides of the string
function songDecoder(song) {
	return (
		song
			// Step 1
			.replace(/WUB/g, ' ')
			// Step 2
			.replace(/\s+/, ' ')
			// Step 3
			.trim()
	);
}

Or we combine Step 1 and Step 2 by replacing all the continuous appearances of 'WUB':

function songDecoder(song) {
	return (
		song
			// Step 1 & 2
			.replace(/(WUB)+/g, ' ')
			// Step 3
			.trim()
	);
}

Solution two - split, filter & join

Another approach to remove all the 'WUB' appearances is to use the split method which converts it into an array, then we’d remove all the empty strings from the array using the filter method and finally we’ll join the remaining words from the array back into a string.

So basically the steps here would be:

  1. Split the string into an array
  2. Filter / remove all the empty strings (' ')
  3. Join the words from the array back into a string
function songDecoder(song) {
	return (
		song
			// Step 1
			.split('WUB')
			// Step 2
			.filter(Boolean)
			// Step 3
			.join(' ')
	);
}

Conclusion

Is there another approach for this challenge? Is something that would you do differently? Let me know 😉

Happy Coding! 😇

    Want to stay updated?
    Sign up for the newsletter.

    No spam. Unsubscribe at any time.