For 循环内 If 语句的 JavaScript 正确语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28618976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript Proper Syntax for If Statement Inside For Loop
提问by DLateef
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:
我尝试将字符串的第一个字母大写,即“hello world”参数应返回“Hello World”。我一直返回一个空白字符串,我认为 if 语句存在一些我无法弄清楚的基本语法错误。代码:
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")
回答by ROMANIA_engineer
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.
- 第一个字符被转换为大写(前面没有空格)
- 当字符前面有空格时,它被转换为大写。否则它保持完全相同。
回答by GrandonBroseph
It appears that your main problem is that your for loop on line 4 is missing an open bracket.
看来您的主要问题是第 4 行的 for 循环缺少一个左括号。
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!"));
回答by Kyle Owen
Other people have already provided the correct solution, but here are a few important points:
其他人已经提供了正确的解决方案,但这里有几个要点:
make sure you indent your code properly, it makes it a lot easier to read and debug.
you need to call the toUpperCase functions using parentheses, aka string.charAt(i).toUpperCase()
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()
确保正确缩进代码,这样可以更轻松地阅读和调试。
您需要使用括号调用 toUpperCase 函数,也就是 string.charAt(i).toUpperCase()
toUpperCase 方法不会修改字符串本身,因此当您调用 output += str.charAt(i) 时,您添加的是原始小写字母,而不是大写字母。你可以看到其他解决方案有这样一行: output += str.charAt(i).toUpperCase()
回答by polizz
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"))
回答by Bergi
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?
我一直得到一个空字符串返回,我认为 if 语句有一些我无法弄清楚的基本语法错误?
Not with the if-statement, but with the for-loop itself:
不是使用 if 语句,而是使用 for 循环本身:
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 afterthe loop (your indentation actually matches this).
这分号手段,没有什么,但在循环体空语句,if语句放在后环(您缩进有与之匹配)。
Use this (fixed also some other problems, like the call to toUpperCase()
and the string beginning):
使用这个(还修复了一些其他问题,比如调用toUpperCase()
和字符串开头):
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"