Global Variable Bug: Apply Functional Programming to Convert Strings to URL Slugs

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

// Add your code below this line
function urlSlug(title) {
  let title_copy = [];
  title_copy = title_copy.concat(title);//.toString();
  console.log((title_copy));
  console.log(typeof(title));
  
  title_copy = title_copy.toString();
  console.log(typeof(title_copy));
  console.log((title_copy));
  console.log(typeof(title));
  console.log(title)
  return title_copy.toLowerCase().trim().split(/\s+/).join('-');
  
}
// Add your code above this line

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

console.log(winterComing);
console.log(globalTitle);`

globalTitle doesn’t change. Yet, it’s telling me it has. What’s going on here?

Even the following solution doesn’t work.

return title.toLowerCase().trim().split(/\s+/).join('-');

It gives me the same feedback that the global variable has changed when it has not.

Why is it doing this?

simple, you have changed the hardcoded value, set it to what it was and you will get that error only if you have code that changes that

the tests are doing your same test, but can’t check what is the value before and after the function, so the tests check with the value they know is the original one

suggestion: next time don’t change pre-created values but change the function call to test with different values

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums