How to delete on space character at the end of table

Hello,
Following this exercice:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/apply-functional-programming-to-convert-strings-to-url-slugs
I increase the difficultie by adding one space character at the end of the string.
But my solution for delete this space doesn’t work and I don’t understand why.

function urlSlug(title) {
let titleLower = title.toLowerCase().split("");
let courante = [];

for (let i = 0; i < titleLower.length; i++) {
 if (titleLower[0] == " ") {
   delete titleLower[0];
 }
 else if (titleLower[i] == " " && courante[courante.length-1] == " "){
   delete titleLower[i]
 } else {
   courante.push(titleLower[i]);
   }
 }
 
  let newTitle = courante.join('').split(" ")
  console.log(newTitle)
  for (let i = 0; i < newTitle.length; i++) {
  if (newTitle[newTitle.length-1] == " ") {
    
     newTitle.pop();
  }
  }
  console.log(newTitle.join("-"))
 
}
urlSlug(" Winter Is  Coming ")

// Only change code above this line

The result of console.log:

![space|433x77](upload://bXtY6gktcd7h81hay8sVY6c41Bu.jpeg)

[ ‘winter’, ‘is’, ‘coming’, ‘’ ]
winter-is-coming-

When I test this litle code with VScode, console.log display one table with length = 3, thus without ‘’ :

let newTitle = [ 'winter', 'is', 'coming', '' ];
for (let i = 0; i < newTitle.length; i++) {
  if (newTitle[newTitle.length-1] == " ") {
    
     newTitle.pop();
  }
console.log(newTitle)

:face_with_thermometer:

You are modifying the array as you iterate over it. This causes indexing problems. Instead of modifying the titleLower array, just populate the new array.

Also, I would try console.log(titleLower) to see what you actually get when you split on spaces with extra spaces. You don’t get a string with a single space in it like you are checking for.

console.log(titleLower)


[ ' ',
  'w',
  'i',
  'n',
  't',
  'e',
  'r',
  ' ',
  'i',
  's',
  ' ',
  ' ',
  'c',
  'o',
  'm',
  'i',
  'n',
  'g',
  ' ' ]

There are all space charaters. Then I delete the first one then the double one.
then it still only one at the end of the “courante” table.

Woops, I though you split on " " instead of "". Have you tried splitting on " ". It would greatly simplify things.

That works now, I made a beautiful mistake.

// Only change code below this line
function urlSlug(title) {
let titleLower = title.toLowerCase().split("");
//console.log(titleLower)
let courante = [];
//console.log(courante)
for (let i = 0; i < titleLower.length; i++) {
 if (titleLower[0] == " ") {
   delete titleLower[0];
 }
 else if (titleLower[i] == " " && courante[courante.length-1] == " "){
   delete titleLower[i]
 } else {
   courante.push(titleLower[i]);
   }
 }
 console.log(courante)

  for (let i = 0; i < courante.length; i++) {
  if (courante[courante.length-1] == " ") {
     courante.pop();
   }
  }
  console.log(courante.join("").split(" ").join("-"))
  return courante.join("").split(" ").join("-")
 
}
urlSlug(" Winter Is  Coming ")

Have you a comment ?

Fortunetly there is trim methode. :stuck_out_tongue_winking_eye:

I still wouldn’t use delete here. It happens not to change the length in this case, but it is a bad idea to get in the habit of modifying arrays as you iterate over them like this. It unnecessarily complicates the logic and it is easy to make changes that result in indexing errors. Also, this approach only handles double spaces.

You have two loops over roughly the same data. You can reduce redundancy and greatly simplify your code if you split on " " instead of splitting on "".

Weird, when I split with " " :

let titleLower = title.toLowerCase().split(" ")

Console.log(titleLower) :

[ '', 'winter', 'is', '', 'coming', '' ]

There is only one space between “is” and "comming.
If I add .join("-"):

Console.log(titleLower) :

-winter-is--coming-

There are two “-” between “is” and "comming. :thinking:

One littel

If you can get rid or skip over these, then you can join to the correct result.

I wanted to say why if I write:

the result is:

And when I write :

let titleLower = title.toLowerCase().split(" ").join("-")

The result is :

With two “-” between “is” and "comming. :thinking:

Why ?

Because - is inserted between every string in the array, to include empty strings…

1 Like

:face_with_thermometer:
ok so it’s normal that there is only one space in the table and two in the string. ok I notice that.
Onther than that you think it’s possible to delete the spaces characters at the begining and the end of the string with for loop and the

?

Somehow, you seem to be making conclusions unrelated to what I’m actually saying, so I don’t think I am being clear enough.


Suppose I have a string

const myString = " this is a  string  ";

If I split on spaces, then I get the words in the string

const myWords = myString.split(" ");

But if you look at the result

console.log(myWords); // [ "", "this", "is", "a", "", "string", "", "" ];

you can see that I ‘accidentally’ added extra spaces in my string.

If I try to join it back up with dashes, then a - gets places between all strings in the array, to include empty strings.

let myDashedString = "-this-is-a--string--";

If I somehow removed the empty strings from my array (without using delete, please") then I would have

const myActualWords = ???;
console.log(myActualWords); // ["this", "is", "a", "string"]

which could be joined up to make

myDashedString = "this-is-a-string";

Code all together
// Split on spaces
const myString = " this is a  string  ";
const myWords = myString.split(" ");
console.log(myWords); // [ "", "this", "is", "a", "", "string", "", "" ];
let myDashedString = myWords.join("-");
console.log(myDashedString); // "-this-is-a--string--"

// Fix split on spaces
const myActualWords = ???;
console.log(myActualWords); // ["this", "is", "a", "string"]
myDashedString = myActualWords.join("-");
console.log(myDashedString); // "this-is-a-string"

Oh yes :upside_down_face: ok, it’s not a good day for me :face_with_thermometer:

Was the new explanation better? Does it make sense? Can I improve it?

No perfect for me.
Now I’m tying to delete the spaces with a for loop …
It’s should be possible

Yup, it is doable. If you use a for loop, I’d recommend pushing good strings into a new array instead of removing bad strings from the current array.

yes at this time, I do that but doesn’t work again

function urlSlug(title) {
let titleLower = title.toLowerCase().split(" ");
console.log(titleLower)
let courante = [];
//console.log(courante)
for (let i = 0; i < titleLower.length; i++) {
 if (titleLower[i] !== " ") {
   courante.push(titleLower[i]);
 } else {
   courante = courante
 }
 }
 console.log(courante)
}
console.log(urlSlug(" Winter Is  Coming "))

Keep in mind though that " " and "" are two different strings. The first one, " ", is a string with one space. The second one, "", is an empty string.

:grinning:

function urlSlug(title) {
let titleLower = title.toLowerCase().split(" ");
console.log(titleLower)
let courante = [];
//console.log(courante)
for (let i = 0; i < titleLower.length; i++) {
 if (titleLower[i] !== "") {
   courante.push(titleLower[i]);
  } else {
   courante = courante
  }
 }
 console.log(courante.join("-"))
}
urlSlug(" Winter Is  Coming ")

That works :partying_face:

Thank a lot, see you tomorrow maybe :wink:

1 Like