Dubstep

Photo by Zachary Smith on Unsplash

Today we are solving a Codewars challenge in JavaScript called Dubstep. The challenge is to identify the original song lyrics to songs that have been remixed by adding “WUB” at various places. Specifically, an arbitrary amount of “WUB”s is added at the beginning of the song, at the end of the song, and at least one is added between each word in the song. So let’s get to work translating some dubstep for the uninitiated.

The input will be a single non-empty string, consisting of only uppercase English letters. The output should be the original lyrics of the song used to make the dubstep remix. Each word should be separated with a space.

So how should be approach this? Looking at the big picture, we are removing values, “WUB”s, from a string, and putting a space where those values where, unless those “WUB”s where before the first original lyric, or the last original lyric. We are returning a manipulated string. A great way to do this is to split the string into an array of substrings, perform further changes if necessary, and then join the array of substrings back into a string.

First, let’s use the split() method to split the string into that array of substrings. The really awesome thing we can do here is that we can use the separator parameter in the split() method to address the “WUB”s. The separator parameter specifies the character to use for splitting the string. This separator is not included in any of the substrings that are returned in the array the split() method returns. So performing .split(“WUB”) on the input removes all of the unwanted “WUB”s.

Where these “WUB”s used to be, there are now empty strings. Our array now contains empty strings, which will be problematic for when we join the substrings back together. We could use the join() method, with a separator parameter, which is similar to the split() method, only this time we’re adding it between the substrings as they are joined into a single string. The issue is that if we join together empty substrings, there will be extra spaces in the returned string.

One way to address this is by using .filter(Boolean) immediately after the split() method. This removes any “falsey” values, which include empty strings or null values. Once this method is applied, only the original lyrics in the song remain in our array.

All that’s left at this point is to convert this array back into a string. We will utilize the join() method with a space within a string as the separator, which will look like: .join(“ “).

This should be saved to a variable that is returned as the output of the array. The entire solution can be found below.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Add Hours to a Date in JavaScript

Palindrome

taroverse-to-hold-ino-via-nftb-launch-how-it-works

Understanding ES6 Arrow Functions

Refer to Array.prototype.map, How to Implement the map method of the Object version?

How To Manage Sessions in Node.js Using Passport, Redis, and MySQL

How to manage sessions in Node.js using Passport, Redis, and MySQL

With just one assertion

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marcos Lipic

Marcos Lipic

More from Medium

A glimpse into my life through the Pomodoro technique!

4 Paintings You Can Add To Your Collection If You Like Andy Warhol’s Pop Art Style

Conversation with Myself

A Letter Next Year’s Freshmen