JavaScript Why my capitalization of javascript string is not working capitalization javascript string

function titleCase(str) {
  var one = str.split('');

  for(var i = 0;i>one.length;i++) {
    one[i] = one[i].charAt(0).toUpperCase() + one[i].slice(1);
  }

  var final = one.join("");
  return final;
}

titleCase("I'm a little tea pot"); // yields "I'm a little tea pot", unchanged

Can you explain to me why it doesn't work? I'm supposed to capitalize first letters of every word in this string.

Answer:1

There are 3 issues in your code,

  • Your for loop is wrong, it should be i < one.length
  • You're not splitting the words but the entire string.
  • You have to put back the space between the words.

    function titleCase(str) {
    var one = str.split(' ');
    
    for(var i = 0;i<one.length;i++) {
        one[i] = one[i].charAt(0).toUpperCase() + one[i].slice(1);        
    }
    
    var final = one.join(" ");
    return final;
}
    
alert(titleCase("I'm a little tea pot"));
Answer:2

I have an html tag, "loginBox" (<loginBox> ... </loginBox>) that drops down when a user selects an option. However, before that it is hidden with display:none. How can I make it drop down, ...

I have an html tag, "loginBox" (<loginBox> ... </loginBox>) that drops down when a user selects an option. However, before that it is hidden with display:none. How can I make it drop down, ...

  1. make element slide in

I am using revision 71. How you can see on images, i can escape some objects in vision, when i am using transparent material. Sorry, i don't know how to explain my problem better. renderer....

I am using revision 71. How you can see on images, i can escape some objects in vision, when i am using transparent material. Sorry, i don't know how to explain my problem better. renderer....

How can I remove an HTML tag, depending on what the content is? For example: <header> <h3> <a href="/category/people/">People</a> <a href="/category/hide/">...

How can I remove an HTML tag, depending on what the content is? For example: <header> <h3> <a href="/category/people/">People</a> <a href="/category/hide/">...

I have a some data that I am using to create many charts. I am using d3.nest to create a key so I can organize my charts by date. Everything seems to be working except that I cannot get the pie ...

I have a some data that I am using to create many charts. I am using d3.nest to create a key so I can organize my charts by date. Everything seems to be working except that I cannot get the pie ...

  1. iterate over objects javascript
  2. iterate over objects js
  3. iterate over objects python
  4. iterate over objects typescript
  5. iterate over objects in array javascript
  6. iterate over objects java
  7. iterate over objects
  8. iterate over objects es6
  9. iterate over objects angular
  10. iterate over objects in list python
  11. iterate over objects powershell
  12. iterate over objects c#
  13. iterate over objects hashmap
  14. iterate over nested objects javascript
  15. iterate over json objects
  16. django iterate over objects
  17. iterate over jquery objects
  18. jq iterate over objects
  19. iterate over json objects python
  20. python iterate over objects in a class