0

TO BE CLEAR: I don't want to know how to capitalize, but rather I want to know why i can change it in one-dimensional, but not 2-dimensional

I'm doing some coding challenges to get familiar with JavaScript.

I capitalized the first Letter of each word in a given string. I split the string into a word-seperated array via String.match(regex);

var word_array = str.match(/\w(\w)*/g);

And I then made from the word another letter-seperated array to change single letters. (also with regex)

letter_array = word_array[i].match(/\w/g);
letter_array[0] = letter_array[0].toUpperCase();

And this works just fine.

But I wanted it a bit shorter, so I tried to do the action on the letter on the second dimension of the word_array, but with no effect at all.

word_array[i][0] = word_array[i][0].toUpperCase();

Full-Code-Snippet

const input = document.querySelector("#string"),
      button = document.querySelector("#DOIT");

button.addEventListener("click", function(){
  LetterCapitalize(input.value);
});

function LetterCapitalize(str) { 
var word_array = str.match(/\w(\w)*/g);
  for(let i = 0; i < word_array.length; i++){
      //This part works
      letter_array = word_array[i].match(/\w/g);
      letter_array[0] = letter_array[0].toUpperCase();
      word_array[i] = letter_array.join("");
      
      //this doesn't
      /*
      word_array[i][0] = word_array[i][0].toUpperCase();
      console.log(word_array[i][0]);
      */
  }
  console.log(word_array);
  str = word_array.join(" ");
  return str;
}
<input id="string" type="text"/>
<button id="DOIT">DO IT</button>
  • See [Capitalize words in string](https://stackoverflow.com/questions/2332811/capitalize-words-in-string). – Wiktor Stribiżew Oct 12 '18 at 09:22
  • Thanks, I'll have a look into it :) Can you explain why my approach didn't work? @WiktorStribiżew –  Oct 12 '18 at 09:24
  • Can you post the whole code? – Matus Dubrava Oct 12 '18 at 09:26
  • @MatusDubrava I added the whole code as working snippet –  Oct 12 '18 at 09:31
  • @D.Schaller Well, the problem is that you are trying to mutate the immutable string, which will not work. In my opinion, this should throw an error because it is definitely a mistake to do that, but that is just how JS works (letting some errors slip silently). – Matus Dubrava Oct 12 '18 at 09:35
  • @MatusDubrava Thank you very much, for your information. Best answer, yet a comment. –  Oct 12 '18 at 09:37

1 Answers1

0

This wouldnt work wouldn't work since Strings are immutable in javascript. the

letter_array = word_array[i].match(/\w/g);
letter_array[0] = letter_array[0].toUpperCase();

code snipped worked as you converted your strings to a list/array which is mutable by nature. although, id like to point out that this question might be a duplicate. here is a capitalization in javascript question

Vincent Pakson
  • 1,891
  • 2
  • 8
  • 17
  • Thank you for your information. But I assure you, that it's not a duplicate, since it's not about capitalizing a word, but why I couldn't use upperCase inside the word_array. Maybe it'll get reopened –  Oct 12 '18 at 10:32
  • oh okay. but then. I guess my answer stays. I prolly misunderstood the question but yes, Strings really are immutable thus the 2-dimensional solution that you stated is wrong. I guess you would have to split the individual words to make it work. :) but all would depend on your case. Goodluck with that! – Vincent Pakson Oct 15 '18 at 02:57