Help needed in understanding solution

Tell us what’s happening:
I have failed to pass the fourth case so far. In the solution 1 ( Solution) it used another filter function, but I don’t understand why is it used, and what it does. Thanks :slight_smile:

Your code so far


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

// Add your code below this line
function urlSlug(title) {
return title.toLowerCase().split(/\s+/).join("-");
}
// Add your code above this line

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36.

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/apply-functional-programming-to-convert-strings-to-url-slugs

the forth case has spaces at the beginning
I suggest you check what’s the value returned after that string is splitted

console.log("  Winter Is Coming".split(/\s+/));

If you do not understand what the filter method does, I suggest reviewing this earlier challenge in the Functional Programming section.

Got it. I somehow missed the first space at the beginning. Thanks for the quick reply

When I use the below code, everything else works but it says that its changing the Global variable, the console.log shows that the Global variable is not changing. What is it that I am missing?

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

// Add your code below this line
function urlSlug(title1) {
let title = title1.slice();
title = (title.toLowerCase().split(/\W+/));
title[title.length-1] == 0 ? title.pop() : null;
title[0].length ==0 ? title.shift() : null;
console.log(title);
console.log(globalTitle);
return title.join("-");

}
// Add your code above this line

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

I need help please.
I my console.log dose not keep the quotation marks. so I’m not passing. Just to see if it was the solution i cam up with i tried out both the first and second solutions and they don’t keep the quotations as well. I don’t know what i am doing wrong.

my solution :
return tittle
.toLowercase()
.split(/\s+/)
.join("-");
i know its very close to the second solution so it must be something I’m not seeing.

Update i found the solution. i wasn’t trimming the empty space. had nothing to do with the quotation like i thought.

but now its saying I’m changing my global variable. witch after a check via console.log i am not.

We need to see your full code.

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 it 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


However, I would really suggest you open your own thread. Click “Get Help” and then “Ask for help”. This will make a forum thread with your code and some information you can fill out.

there is a line that say to change code only below that line

if you manually changed the original global variable you also get that error

@jasonlei.chn @morningstar

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

We have set your post to unlisted. Thanks for your understanding.

this is my way

function urlSlug(title) {

    var splited = title.split(" ")

    var filtered = splited.filter(word => word.match(/^\S+/g) ||  word.match(/\S+$/g))

    return filtered.join("-").toLowerCase()

This is a way to solve the challenge without using .map/.reduce or .filter methods:

function urlSlug(title) {
var toLower = title.toLowerCase();
var newStr = toLower.trim(); 
newStr = newStr.split(/\s+|\W/).join("-");
return newStr;

}

I do this

function urlSlug(title) {

  return title.match(new RegExp(/\w+/,"g")).join("-").toLowerCase();

}

@ahmeteneskcc @Gaxis @Regismrs

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.