1

I try to capitalize the first letters of a string, i.e. "hello world" argument should return "Hello World". I keep getting a blank string returned, I think there is some basic syntax error with the if statement that I can't figure out. Code:

function LetterCapitalize(str) { 
 var output = " "
 for(var i=0; i < str.length; i++);
 if(str.charAt(i - 1) == " ") {
 str.charAt(i).toUpperCase;
 output += str.charAt(i);
 } else {
  output += str.charAt(i);
 }
 return output         
}
LetterCapitalize("hello world")                      
1
  • toUpperCase is a function. It needs parentheses to be called. Also, it won't modify the string, but will return the uppercased letter. Commented Feb 19, 2015 at 23:28

5 Answers 5

2

Other people have already provided the correct solution, but here are a few important points:

  1. make sure you indent your code properly, it makes it a lot easier to read and debug.

  2. you need to call the toUpperCase functions using parentheses, aka string.charAt(i).toUpperCase()

  3. the toUpperCase method does not modify the string itself, so when you call output += str.charAt(i), you are adding the original lowercase letter, not the uppercase letter. you can see the other solutions have the line: output += str.charAt(i).toUpperCase()

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

function LetterCapitalize(str) { 
     var output = ""+str.charAt(0).toUpperCase();
     for(var i=1; i < str.length; i++){
         if(str.charAt(i - 1) == " ") {
             output += str.charAt(i).toUpperCase();
         } else {
             output += str.charAt(i);
         }
     }
    return output;
}
console.log(LetterCapitalize("hello world"))

Explanation:

  • The first character was converted to upper case (no space before it)
  • When the character was preceded by a space, it was converted to upper case. Otherwise it remained exactly the same.

Comments

1

It appears that your main problem is that your for loop on line 4 is missing an open bracket.

Working code:

function letterCapitalize(string) { 
  var output = " ";
  var newWord = true;
  for(var i = 0; i < string.length; i ++){
    if(newWord){
      newWord = false;
      output += string[i].toUpperCase();
    } else output += string[i];
    if(string[i] === " ")newWord = true;
  }
  return output;
}
console.log(letterCapitalize("hello world!"));

Comments

0

I keep getting a blank string returned, I think there is some basic syntax error with the if statement that I can't figure out?

Not with the if-statement, but with the for-loop itself:

for(var i=0; i < str.length; i++);
//                               ^

This semicolon means that there is nothing but an empty statement in the loop body, the if-statement is placed after the loop (your indentation actually matches this).

Use this (fixed also some other problems, like the call to toUpperCase() and the string beginning):

function letterCapitalize(str) { 
    var output = "";
    for (var i=0; i<str.length; i++)
        if (i == 0 || str.charAt(i-1) == " ")
            output += str.charAt(i).toUpperCase();
        else
            output += str.charAt(i);
    return output;
}
letterCapitalize("hello world"); // "Hello World"

Comments

0
function LetterCapitalize(str) {
    var output = "";
    for (var i = 0; i < str.length; i++) {
        if (i === 0) {
            output += str.charAt(i).toUpperCase();
        } else {
            output += str.charAt(i);
        }
    }
    return output;
};

console.log(LetterCapitalize("hello world"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.