0

The purpose of this code is to make the first letter of every word passed to the function uppercase, but it end up returning uppercase for all position were the first letter is repeated.

function c(x) {
  var y, z = x[0];
  y = x.split(z).join(z.toUpperCase());
  return y;
}
console.log(c("emmanuel"));

2 Answers2

0

    function c1(x) {
        return x.charAt(0).toUpperCase() + x.slice(1);
    }



    function c2(x) {
        return x[0].toUpperCase() + x.slice(1);
    }
    
    console.log(c1("test string"));
    console.log(c2("test string"));
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Asolace
  • 1,006
  • 8
  • 9
0

function c(x) {
  var ARR;
  ARR = x.split("");
  ARR[0]=ARR[0].toUpperCase();//<<< then you should also 
                              //uppercase just 
                              // the first char
  return ARR.join("");
}

console.log(c("test string"));

there is also a css rule which do the trick... css capitalize

same just for a whole sentence

            <script>
                function titlelize(s) {
                    var ARR,WORDS;
                    WORDS = s.split(" "); // separate sentencein words

                    WORDS[i]=WORDS[i].toLowerCase();//ensure all other will be lowercase
                    for (var i = 0; i < WORDS.length; i++) { //run over each word
                        ARR = WORDS[i].split(""); // split word in chars
                        ARR[0] = ARR[0].toUpperCase() //make first car uppercase
                        WORDS[i] = ARR.join(" "); //recreate word from char
                    }
                    return WORDS.join(" ") // recreate sentence
                }
            </script>
Thomas Ludewig
  • 696
  • 9
  • 17