Title Case a Sentence nothion is defined

for Pete sake what is happening here? I keep changing and nothing is working:face_with_symbols_over_mouth:

Your code so far


function titleCase(str) {
  var myStr=(arr.splice(' '));
  var arr=0
  for (var i=0; myStr.length;i++){
    myStr.toLowerCase();
    myStr.replace (0,myStr.toUpperCase())
  } 
  return myStr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0.

Link to the challenge:

String methods return a new string rather than modifying the original, so you have to do something like

myStr = myStr.toLowerCase();

Check the appropriate parameters to replace()

The middle expression in a for statement needs to be a Boolean expression, not just a number, so the for loop will stop when the expression becomes false.

Actually, it just needs to be an expression. Technically a number would still be evaluated as an expression. It won’t help in this particular for loop, but the following would be totally valid.

var arr = [1, 2, 3, 4];
var sum = 0;
for (i = 0; arr.length; i++) {
  sum += arr[i];
  arr.splice(i,1);
  i--;
}
console.log(sum); // 10

The arr.length gets evaluated as true as long as the length of the array is greater than 0. Since 0 is falsy, the for loop will stop once there are no more elements in it.

@rjk welcome to the forum.

@RandellDawson
yea its unfinished i need to see what its doing so i can finish and i don’t know how to see it

When you want to “see” what is going on with your code, just add console.log statements and look at your browser’s console. For example, try the following (using your existing code) to see what happens. I have also added some comments throughout your code.

function titleCase(str) {
  var myStr=(arr.splice(' ')); // There is no variable named arr defined so you an not splice, plus why are you using splice?
  console.log('myStr = ' + myStr);
  var arr=0 // what is the purpose of this line? 
  for (var i=0; myStr.length;i++){
    myStr.toLowerCase(); // this returns the entire myStr string in lowercase form, but since you do not assign it to anything, nothing useful happens
    myStr.replace (0,myStr.toUpperCase()) // this line returns a new string with the first "0" replaced with the uppercase version of myStr.  However, since there is no "0" in it there is no point and you never assign this returned string to anything, so nothing useful happens.
  } 
  console.log(myStr);
  return myStr;
}

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

I’d like to add once you sort the variable declaration and follow Dawsons advice with console logging.
If you’re unsure why something isnt working at all double check the methods you are calling on MDN or w3schools etc. It will definetly help solve some of these quicker. :slight_smile:

yea that is what I’m getting to I can scrap it. I think I’m just not going to get JavaScript. it’s sad. I did well on the other stuff.

You can it’s just not as straight forward as HTML and CSS. For instance, when you have loops write each step out, use a console.log inside the loop to print what is happening to understand what is going wrong.
Thing about what the code needs to do step by step.

  1. Split the string into words
  2. Make sure the string is all lower case
  3. Capitalise each words first character
  4. Return the new words as a string

Look up methods on MDN for strings and Arrays, how to get each word individually, how to get the first character of each word, how to change that character.
When you’re looking at these method read what the parameters need to take and then check again what you’re giving it by console logging before you call the method.

https://www.w3schools.com/jsref/jsref_obj_string.asp

I don’t think it is JavaScript per say. I think you are having trouble writing the code, because you don’t have a clear algorithm before you start writing the code. If you don’t have the correct algorithm already sorted out, writing code is going to just be “guessing” and “hoping” you get the right code.

I’m just not feeling it now I was enjoying it. like my console.log don’t work I watch other people on youtube and all their stuff works so I try and it doesn’t I don’t understand and I feel like washout before I start. I just don’t know what to do.

Everyone starts somewhere, 99% of professional developers use google daily but they’re just good at googling it and searching MDN etc.

If you really aren’t enjoying it take a break do something you are enjoying and then come back if you want to start again.

i’m looking at new ways now may one will work for me

These methods don’t change the value, instead they return a new value. You can chain those methods if you like. Also, you can’t do that on an array, it should be myStr[i].whatever.

i < myStr.length; the i < is missing.

What does splice do? To split the string, use arr.split(' ');.

There are more issues, but I’ll get to them later.

I think I got it


[details="Summary"]
function titleCase(str) {
  var myArr = str.toLowerCase().split(' ');
//assigns new array to sting in lower and split
  for (var i = 0; i < myArr.length; i++)
  //assigns i to first character then counts 
   {
    var j = []
    //placeholder
     {
      myArr[i] = myArr[i].replace(myArr[i].charAt(0), function replace(j) {
        return j.toUpperCase();
        /* alot happen here the arr that is counted is replaced and then place in j
        then returned in upper case so now all the first letters or in caps and in j*/
      });
      console.log(myArr);
    }

    return myArr.join(' ');
  }//return the array back to together with caps 
}
titleCase("I'm a little tea pot");
[/details]

Well, that just should be at the end of the function body, not inside the for loop. Done!

yea I like the short ones too but I had to walk this one out

I don’t remember a map whats that do

It didn’t work the last return not working