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.