0

I'm trying to detect which word is a name, and capitalize the first letter of it. Here's the code I've tried:

else if (splitInput[i].match(/a|e|i|o|u/)){
        i = 0;
        var up = input.indexOf("ben");
        up = up + 4;
      console.log("toUpperCase: " + up);
        i = slotBen + 1;
        var toUp = input.charAt(up);
      console.log("Before uppercase: " + toUp);
        var afUp = toUp.toUpperCase();
      //input.charAt(up).replace(toUp, afUp);
      console.log("After toUpperCase: " + afUp);
        //input.replace(toUp, afUp);
      console.log(input);
        bubbleR.innerHTML = input;
        console.log('slotBen:', slotBen);

      userID.name = splitInput[i];

Maybe a little too complex, but I couldn't make it work or easier to read/understand. If you run the function, it knows exactly which letter it has to capitalize, but it doesn't replace it in input. Here's a codepen

Text is in Dutch, but don't mind that :)

Edit: had the suggestion to try this:

var capitalized = splitInput[i].toUpperCase() + input.substr(1);

with i being i = slotBen + 1;, but it didn't work. I think I did implement it right, but I'm not sure. Tried different combinations and none worked out.

Sam B.
  • 5
  • 4
  • You might want to add spaces after and before 'ben' because now, it'll uppercase any occurance of 'ben' ( which would result in words as Benign going capitalized as well) – roberrrt-s Sep 05 '16 at 11:34
  • What is `slotBen`? Anyway, JS strings are immutable; you cannot "replace" characters. You have to create a new string. `replace` does not replace characters in an existing string; it creates a NEW string with the specified replacement. This should all be fairly clear if you read the documentation. More generally, searching for "capitalize word in JavaScript" should turn up LOTS of approaches to this. –  Sep 05 '16 at 11:34
  • Would CSS be an option? There is a text-transform: capitalize; – moefinley Sep 05 '16 at 11:38
  • @moefinley I don't know, couldn't find a suitable solution – Sam B. Sep 05 '16 at 11:39
  • The simplest way to capitalize a string is something like: s.substring(0, 1).toUpperCase() + s.substring(0, 1) – Greg Sep 05 '16 at 11:39
  • @torazaburo I've searched for capitalize word in JavaScript and almost every other related term, I even went full retard and got to page 2 on Google and couldn't find anything. – Sam B. Sep 05 '16 at 11:41
  • @Greg It looks like it could work, but I don't know how to implement it because I'm not really good at modifying strings – Sam B. Sep 05 '16 at 11:43
  • Can you share the full if else condition through fiddle – ajaykumar Sep 05 '16 at 11:44
  • @ajaykumar I'll share my full code in a fiddle – Sam B. Sep 05 '16 at 11:45
  • When I searched for "capitalize word JavaScript", the first half-dozen results looked useful, including the [first one](http://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript). –  Sep 05 '16 at 11:50
  • I added a link to codepen because the function didn't work in jsFiddle – Sam B. Sep 05 '16 at 11:50

1 Answers1

0

The way to capitalize first letter. Give it a try.

   //forFirstLetter 
function capitalize(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

console.log(capitalize("demo"))
ajaykumar
  • 646
  • 7
  • 17