简体   繁体   中英

JavaScript Proper Syntax for If Statement Inside For Loop

I try to capitalize the first letters of a string, ie "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")                      

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.

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!"));

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()

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"
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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM