Unctional Programming: Apply Functional Programming to Convert Strings to URL Slugs

I am having an issue on this problem where I cant pass all the tests at once. If I change the globalTitle variable to the various quotes each of those tests will pass individually, however they wont all pass at once. Anyone know whats up??

globalTitle -> mutableTitle -> noSpaceBeginning -> noSpaceMiddle -> frankenVar

// the global variable
var globalTitle = "Winter Is Coming";

// Add your code below this line
function urlSlug(title){
    //Change globalTile to a mutable variable
    let mutableTitle = "";
    for (let i = 0; i < globalTitle.length; i++){
        mutableTitle += globalTitle[i];
    //Regex to remove spaces at the beginning
    let beginningRegex = /^\s/;
    let noBeginningSpace = "";
    let beginningTest = beginningRegex.test(mutableTitle);
    if (beginningTest) {
        for (let j = 1; j < mutableTitle.length; j++){
            noBeginningSpace += mutableTitle[j];
    }else {
        for (let i = 0; i < globalTitle.length; i++){
        noBeginningSpace += mutableTitle[i];
    //Regex to remove consecutive spaces in middle
    let midRegex = /\s\s/;
    var noSpaceMiddle = "";
    let midTest = midRegex.test(noBeginningSpace);
    if (midTest) {
        for (let i = 0; i < noBeginningSpace.length; i++) {
            if (noBeginningSpace[i] === " " && noBeginningSpace[i+1] === " "){
                var myIndex = i;
        for (let j = 0; j < noBeginningSpace.length; j++){
            if (j === myIndex){
            noSpaceMiddle += noBeginningSpace[j];
        for (let i = 0; i < noBeginningSpace.length; i++){
        noSpaceMiddle += noBeginningSpace[i];
    //Convert string to no spaces, lowercase, and connected by "-"
    let frankenVar = noSpaceMiddle.split(" ").join("-").toLowerCase();
    return frankenVar;
// Add your code above this line

var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"
1 Like

So you fell down a very common mousehole – you’re using the global variable (globalTitle) throughout your function. This is what is known in the industry as “a bad thing.”

See, you’re passing all the ‘winter-is-coming’ tests, because no matter what happens, you’re accessing the global variable which remains set to ‘Winter Is Coming’. But here’s a question for you: which test fails?

urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone") is returning ‘winter-is-coming’. Why is that?

The urlSlug() has a parameter signature - it has a certain pattern or set of parameters being passed in. In this case, it is creating a title variable within your urlSlug(), which is what you want to use, rather than accessing any global variables inside your function.

Short form takeaway? You can’t rely on the existence or continuity of global variables. Don’t use them, if you can avoid it. And, if you’re on the ‘Functional Programming’ lessons, this is doubly important. The first point of functional programming is, when I pass a certain value in, I will always get a certain value out. If you’re relying on a global variable, which anything can change, you can’t guarantee you’ll always get the same result.

1 Like

It’s always the silliest things. Thank you very much!

1 Like