Basic Algorithm Scripting - Title Case a Sentence

I know there are a ton of solution but I want to do by my self, and I’m stuck in this problem, I already try by different ways as using loops, (for), and flowing the steps of freeCodeCamp. First split(" ") into an array, then transforming it to lowerCase and then to upper but nothing works for me. And this idea, I know is not the best, but I don’t get way the console display the first “t” that he found, and not the “t” then to the space, which is the condition given to the if();
Any Idea ?

  function titleCase(str) {
    
    for(let i = 0; i < str.length; i++){
    if(str[i] === " "){
      str = str.replace(str[i + 1], str[i + 1].toUpperCase()); 
      }
    }
    return str; 
}
  
  console.log(titleCase("I'm a little tea pot"));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

hi there, it is great that you are trying to ignore the solutions on the internet and instead focusing on solving this yourself.

A good method to solving things is to begin by writing out an algorithm.
An algorithm is a series of detailed steps that explain how someone would solve this problem. Preferably you would write it as if the person who is solving the problem is very young, so nothing complex.

Example of an algorithm that takes two numbers and multiplies them without using multiplication but using addition for eg is:

  • take 2 numbers called num1, num2
  • if num2 is 2 or greater, add num1 to itself as many times as num2 says to do it minus 1
  • if num2 is 1 return num1
  • if num2 is 0 return 0
    finally return the sum calculated earlier

Therefore an algorithm is a kind of roadmap for how we want to tackle a problem.

Given this information, let me ask you, do you have an algorithm for how you want to solve this problem? Please list it.

Instead of updating the old string, why not build a new string? Replace doesn’t work the way you want it to here.

Oh really thank you, It really helped me to understood the issue I was having. <3

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.