Basic Algorithm Scripting: how to remove an item properly from the object?

Dear freeCodeCampers,

I am struggling with this exercise: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence/

code: https://codepen.io/BartekSkrzyczek/pen/ZVoBWa

I commented out line 14, because it does work properly. My intention was to remove an item from the array based on str [i], but it does not work properly.

Could you please help me to remove letters which were capitalized later? Cheers!

Wow. Seems to me you might want to rethink your strategy. You’re going through letter-by-letter, when you may not really need to. You know a couple of things:

  1. you’ll get a string of words, possibly of mixed case.
  2. for EACH word, the first letter should be upper case.
  3. for EACH word, the rest of the letters should be lower case.
  4. you will return a string, containing each word with the above modifications.

Does this indicate something? Perhaps splitting the string into an array of words, then doing something with each word individually?

Another strategy would be to split the array into words, then for each word process them separately.

2 Likes

Another suggestion? When you’re dealing with pure javascript (rather than JS and HTML and/or CSS), you may find something like repl.it is more suitable. You can choose an pure-JS environment, and it’ll set you up with an editor and a displayed console.

As this is not a final challenge, I don’t mind sharing the code I put together for this, if you think it might help. Take a look at this repl.

1 Like
  • Start by making the whole string lowercase, easier to compare.
  • Convert the string to an array by using split, this will make you be able to loop each word in the string.
  • Use console.log() here and there, to check if you get the result you intend to before moving further with the code.
  • .map() would be useful here.
1 Like

Thank you. I will try this way also.

This is an interesting approach. I will try to write the code based on your steps.

Here’s a strategy. You’ll want to break a sentence into an array of words and go over each word. You want to make sure the first letter is uppercase and the rest of the letters are not.

Hint: String.prototype.split() and String.prototype.substring()

1 Like

String.replace() is also a fun option.

2 Likes