-1

I am learning JavaScript and I am disappointed that the below code doesn't work. It is still a lowercase "i". I was trying to capitalize every letter in the string (hence the for loop) but I wanted to stop and address what was going wrong.

str = 'i ran there';

for(var i = 0; i<str.length; i++){
  if(i===0){
    str[i] = str[i].charAt(0).toUpperCase();
  }
}


console.log(str);

Can someone please describe what is going wrong here?

khant vyas
  • 123
  • 3
Jake.JS
  • 3,266
  • 3
  • 15
  • 18

4 Answers4

0

You have to create a new string because strings in javascript are immutable:

First get every word separated:

var arrayOfstrings = s.split(" ");

Then you can treat each string like there own word

Fancy way :

 var capFirstLetter = arrayOfStrings[index].replace(/^./, function (match) {
    return match.toUpperCase();
});

Which is just a regex. The /^./ means the first character in the string. And the rest is self explanatory.

Or this way:

var s = arrayOfStrings[index];
var s2 = s[0].toUpperCase()+ s.substr(0,1);

Or even this really lame way

var s = arrayOfStrings[index];
var newS = "";
for(var i = 0; i < s.length; i++){
  if(i == 0) newS+= s[0].toUpperCase();
  else newS+= s[i];
}

Of course all these can be done in a forloop to cap them all and put back together:

 var s = "hello woorld hello world";
 var arrayOfStrings = s.split(" ");
 for(var i = 0; i < arrayOfStrings.length; i++){
    arrayOfStrings[i]= arrayOfStrings[i].replace(/^./, function(match) {return match.toUpperCase();}); 
  }
 var s2 = arrayOfStrings.join(" ");
Jay
  • 2,656
  • 1
  • 16
  • 24
0

If I understand your question, you could do it with something like -

var str = 'i ran there';
var arr = str.split(" ");
var div = document.getElementById("out");
for(var i = 0; i < arr.length; i++){
  // The First letter
  // arr[i] = arr[i].substring(0,1).toUpperCase() + arr[i].substring(1);
  // Every letter
  arr[i] = arr[i].toUpperCase();
  div.innerHTML += arr[i] + "<br />";
}
<div id="out"></div>

As for what is going on in your code, you can't modify the array backing the String (assuming it is an array) like that.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

whats going wrong here is strings in javascript are immutable.

You can't change them. What you can do is create a new string with the change.

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
0

You are asking for index i of a String str. Because str is a String, you cannot use index values to grab certain characters in the string - try console.log(str[0]) - it will return undefined.

To accomplish what you are trying to do, you would need to simply add to a new string after capitalizing each letter. Example:

str = 'i ran there';
capStr = ''

for(var i = 0; i < str.length; i++){
    if(i === 0) capStr += str.charAt(i).toUpperCase();
    else capStr += str.charAt(i);
}

console.log(capStr);
turner
  • 1,667
  • 2
  • 13
  • 21
  • This is basically the right answer/what I am looking for. However, if you add an if/else statement in the for loop (before the capStr line) to check if (i===0) that would be exactly what I am looking for. Thanks!! – Jake.JS Sep 19 '14 at 02:38