Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:
Describe your issue in detail here.
Why will this syntax not work?

  **Your code so far**
function titleCase(str) {
let moddedStr = str.split(" ");

for (let i = 0; i < str.length; i++) {
  moddedStr[i][0].toUpperCase();

  for (let j = 1; j < str[i].length; j++) {
    moddedStr[i][j].toLowerCase();
  }
}

return moddedStr;
}

console.log(titleCase("I'm a little tea pot"));
  **Your browser information:**

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

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

for (let i = 0; i < str.length; i++) {
  moddedStr[i][0].toUpperCase();

You are creating your index based on str, but are using it to index through moddedStr? If you are trying to decide how many door knobs you need for house A, you don’t do it based on how many doors house B has.

Also, be careful what you are returning.

function titleCase(str) {
  str.split(" ");

  for (let i = 0; i < str.length; i++) {
    str.toUpperCase();

    for (let j = 1; j < str[i].length; j++) {
      str.toLowerCase();
    }
  }

  return str;
}

console.log(titleCase("I'm a little tea pot"));

Now the problem is nothing gets modified.

Right. Prototype methods for strings never modify the original - they return a new value.

The idea that you had before was fine, you were just looping incorrectly.

Put in some console.log statements so you can see what is happening.

function titleCase(str) {
let moddedStr = str.split(" ");

for (let i = 0; i < moddedStr.length; i++) {
  console.log(moddedStr[i][0].toUpperCase());

  for (let j = 1; j < moddedStr[i].length; j++) {
    console.log(moddedStr[i][j].toLowerCase());
  }
}

return moddedStr;
}

console.log(titleCase("I'm a little tea pot"));

console.log was a game changer. This is my progress so far. Now it’s just a matter of building onto my code.

OK, keep working through it. Break the problem into small problems and solve them one by one. If a subproblem is too big, break it down.

You have the first step - you’ve broken the sentence into individual words. Now you need to figure out what to do with those.

function titleCase(str) {
let moddedStr = str.split(" ");
let blankStr = "";

for (let i = 0; i < moddedStr.length; i++) {
  console.log(blankStr += moddedStr[i][0].toUpperCase());

  for (let j = 1; j < moddedStr[i].length; j++) {
    console.log(blankStr += moddedStr[i][j].toLowerCase());

  }
}

return blankStr;
}

console.log(titleCase("I'm a little tea pot"));

Here is my progress so far. I’m familiar with appending, so getting to this part is easy. Now I ended up making an entirely new question to solve, but I’m gassing out on easy ways to solve this one. I briefly googled and saw a regular expression solution, I looked away so I don’t copy paste the solution, but I’m not comfortable with regular expressions.

OK, that’s a different approach than I’m used to seeing. That can work. I was able to make changes on two lines and get this to pass.

One thing - I would avoid changing data inside the log statments:

console.log(blankStr += moddedStr[i][0].toUpperCase());

should be

blankStr += moddedStr[i][0].toUpperCase()
console.log(blankStr);

to avoid confusion.

Also, building strings like this, one character at a time can be resource consuming for the computer - it has to allocate new memory and deallocate the old for each iteration. It’s fine for what you are doing here - just be aware.

So, you are almost there. How are you going to get spaces in there?

but I’m not comfortable with regular expressions.

Only Vulcans are, and only on a good day. Don’t get me wrong, regex is amazing and sometimes can do a lot in a few lines of code, but yeah, it takes a while to get used to it.

function titleCase(str) {
let moddedStr = str.split(" ");
let blankStr = "";
let secondBlankStr = "";

for (let i = 0; i < moddedStr.length; i++) {
  console.log(blankStr += moddedStr[i][0].toUpperCase());

  for (let j = 1; j < moddedStr[i].length; j++) {
    console.log(blankStr += moddedStr[i][j].toLowerCase());

  }
}

const capitalLetterArray = blankStr.split(/(?=[A-Z])/); //cheated for regular expression syntax.

for (let i = 0; i < capitalLetterArray.length; i++) {
  if (i < capitalLetterArray.length - 1) {  //maybe this could look prettier
  secondBlankStr += capitalLetterArray[i] + " ";
  } else {
    secondBlankStr += capitalLetterArray[i];
  }
}

console.log(capitalLetterArray.length);

return secondBlankStr;
}

console.log(titleCase("I'm a little tea pot"));

This was my complete solution. Yikes, you can roast me on this one.
I mainly kept using what I’m comfortable with. That is for loops, arrays, and appending…

I wrapped that in [spoiler][/spoiler] tags since it is a working solution to a curriculum challenge.

Cool, good job.

Is it the best solution? Probably not. But the more you do algorithms, the more you’ll realize that there is usually someone out there with a better solution.

The important thing is that this is your solution and you solved it with your knowledge. That is the value here - the working the problem.

Now would be a good time to take a look at other solutions to see how they handled it - that can be a great learning tool.

Good job, keep up the good work.

Just for reference, this is how I fixed your code:

function titleCase(str) {
  let moddedStr = str.split(" ");
  let blankStr = "";

  for (let i = 0; i < moddedStr.length; i++) {
    if (i === 0) 
      blankStr += moddedStr[i][0].toUpperCase()
    else
      blankStr += ' ' + moddedStr[i][0].toUpperCase()

    for (let j = 1; j < moddedStr[i].length; j++) {
      blankStr += moddedStr[i][j].toLowerCase()
    }
  }

  return blankStr
}

There are ways to clean up that logic but I didn’t want to use notation to which you have not yet been exposed.

I looked at the Hint solutions and they came up with syntax (or notation rather, as you put it), that I wouldn’t have been able to come up with on my own no matter how long I stared at the screen.
Maybe if I took thorough notes and referred to it on a piece of paper, I could do something like that.

function titleCase(str) {
  const newTitle = str.split(" ");
  const updatedTitle = [];
  for (let st in newTitle) { //Was there a lesson I did for this one? I did everything in order up until this module.
    updatedTitle[st] = newTitle[st][0].toUpperCase() + newTitle[st].slice(1).toLowerCase();
  }
  return updatedTitle.join(" ");
}

No curly braces on if else statements?

Yeah, algorithms are like tools in your tool belt. You just keep learning new ones and getting better.

for (let st in newTitle) { //Was there a lesson I did for this one? I did everything in order up until this module.

It may not have been covered yet. I don’t know if it is. When in doubt, check the docs. Seriously, the best tool a developer has is google. On a good day I only use it a dozen times. MDN is a great source for JS. Google “mdn for in”. You could easily replace that with a “regular” for loop.

Don’t get flustered. This is hard stuff. That’s why it pays well. Just keep moving forward.

They aren’t required if you have only one statement - they are optional in that case.

Let’s say they threw a novel concept at me. I guess I’ll cheat using google?
That is for answering the smaller questions until I come up with my own answer.

I’ll do everything I can to avoid the hint, which is going to give the whole answer away. Although, if they use unfamiliar notation, I don’t find it that helpful. Things that I don’t use or I once-overed I avoid using or I feel like a “fraud”.

I know my initial question is solved, but another important tangent question is basically how should I approach researching that isn’t counterproductive (cheating)?

Looking up the details of the language is not cheating. The purpose of studying algorithms is to learn algorithms. That is independent of the language. It is common to discuss algorithms with people that don’t do the same computer languages. Heck, you should be able to explain simple algorithms to people that don’t even code.

Google the language all you want. Google “what is a for in loop”. Don’t google “how do i title case a sentence in javascript”.

I’ll do everything I can to avoid the hint, which is going to give the whole answer away.

I know it’s tough, but the more you struggle, the more you learn.

Two guys go to two different survivalist camps. One is on a beach in the Caribbean with nice weather, a fresh water spring, and fruit dangling from the trees. The other is on a cold and dry and barren mountain, with little food, and roving packs a wolves to keep you on your toes. One is going to have a better time, but the other is going to learn more.

Although, if they use unfamiliar notation, I don’t find it that helpful.

OK, but it’s a chance to learn that notation, or at least get exposed to it.

… or I once-overed I avoid using …

That is 100% normal. There are too many things along two many paths. You can’t explore every path in the forest at once. You go down one. You see paths branching off on each side and you say, “OK, I’ll come back and check those out later.”

I keep a notepad next to my desk. When I see something about which I want to learn more but don’t have time right now, I write it down. Then I come back later.

… or I feel like a “fraud”.

We all feel like a fraud sometimes, especially in the coding world. Google “imposter syndrome”.

I know my initial question is solved, but another important tangent question is basically how should I approach researching that isn’t counterproductive (cheating)?

Just struggle as long as you can. “Cheating” happens. (When I’m alone and night, I sometimes cry into my pillow and admit that I have been defeated by a few algorithms that I just could not solve.) If you find you have to peek, one way to mitigate that is to come back a week later and solve the challenge from memory. You lost the benefit from the struggle, but at least you are making sure that you really learned it.

Again, this is hard stuff. Go easy on yourself.