Title Case a Sentence help + Basic Algorithm Scripting problems in general

Tell us what’s happening:
hi im unsure of how to capitalize each letter now that i have each word in an array.

also in general i feel the challenges are paced wrongly (?)
im using functions i haven’t learnt before in the earlier parts in order to solve the challenges posted in this portion of the curriculum… is this normal? or am doing all these challenges wrongly?
i feel logically i can understand how to solve the challenge but i lack the “tools” to…?

is my approach to learning javascript wrong? thank you!

Your code so far


function titleCase(str) {
  let strLower = str.toLowerCase();
  let strLowerArr = strLower.split(" ");
  for (let i=0; i<strLowerArr.length; i++)
  {
    strLowerArr[i][0].toUpperCase();
  }
  let ans = strLowerArr.join(" ");
  return ans;
}

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/74.0.3729.131 Safari/537.36.

Link to the challenge:

Hi. The mistake is in this line:

strLowerArr[i][0].toUpperCase();

I understand that you are trying to “modify” the string at the i-th index. But strings are immutable, so you cannot modify a string with a method.

Do you think you can take it from there?

2 Likes

Additional hint: Take a look at how you used toLowerCase() in line2.

1 Like

Hello @xnmng,

In general I’ve found that the freeCodeCamp challenges can stretch the limits of what you have been taught, but the necessary tools have usually been introduced. Sometimes they want you to learn by searching for the answers. In addition to completing the freeCodeCamp exercises and projects, I would strongly recommend reading documentation, Medium articles, and watching YouTube videos. Sometimes a text-based explanation on FCC is not enough for me, so I oftentimes look up video explanations to gain another point of view.

I like to think of freeCodeCamp as a roadmap. FCC does a great job at introducing topics, but I think you’ll need additional practice at nearly every concept to become proficient.

Cheers,
Austin

1 Like

i turned the whole string into lower case, then split the string into an array with each element containing a word

i understand the function isnt meant to be used like this after reading the documentation

what i was trying to do is refer to the first letter of each array element then change that to caps

any advice how i can achieve this?

Your idea is fine. Let me see if this example makes it clear:

let x = "hello";
x.toUpperCase();
console.log(x); // prints "hello"

In otherwords, toUpperCase() does not change the value of x, which is a String, which is immutable.

strLowerArr[i][0].toUpperCase(); returns the uppercase of the first letter of the string at strLowerArr[i], but it does not modify the string itself.

// Let's say that strLowerArr[i] is "hello" //
strLowerArr[i][0].toUpperCase(); // "H" is returned, but is forgotten. strLowerArr[i] is still "hello"
1 Like

function titleCase(str) {
let strLower = str.toLowerCase();
let strLowerArr = strLower.split(" ");
let strLowerArrRet = [];
for (let i=0; i<strLowerArr.length; i++)
{

strLowerArrRet[i] += strLowerArr[i][0].toUpperCase();
for (let j=1; j<strLowerArr[i].length; j++)
{
  strLowerArrRet[i] += strLowerArr[i][j];
}

}
console.log(strLowerArrRet)
let ans = strLowerArrRet.join(" ");
return ans;
}

titleCase(“I’m a little tea pot”);

i modified my code but the console log gives me alot of undefined… im unsure why is that?

Hello @xnmng,

It looks like your code is a bit more complicated than you need. The second for loop is confusing and unnecessary. Try to concatenate the upper case letter with the rest of the original word and push to strLowerArrRet in the same for loop.

Right now your function returns undefined sometimes because this line strLowerArrRet[i] += strLowerArr[i][j];. You are incrementing j for each loop, which is soon exceeding the length of the words. But you don’t need this second for loop.

1 Like

Hi there
Let’s take a simple example.
Think about how this connects to the loop.

  1. Get the first index in the string.
  2. Change the character to uppercase
  3. Link the string after index (0).
  4. Convert the completed array to a string.

The function is not complete.

function titleCase(str) {
    return  charAt  + toUpperCase  +  slice
}

titleCase("fccveryfun!");
1 Like

thank you!!

function titleCase(str)
{
let strLower = str.toLowerCase();
let strLowerArr = strLower.split(" “);
let strLowerArrRet = [];
for (let i=0; i<strLowerArr.length; i++)
{
strLowerArrRet[i] = strLowerArr[i][0].toUpperCase();
strLowerArrRet[i] += strLowerArr[i].slice(1);
}
let ans = strLowerArrRet.join(” ");
return ans;
}

would there be any way to improve my solution to this problem?

You could not create the second array and just substitute the elements of the first array while you are iterating over it so that it requires less memory

1 Like

thanks!

so i just return strLowerArrRet.join(” ");?

I meant you could avoid creating strLowerArrRet and just change the elements of strLowerArr