【发布时间】:2018-07-09 14:39:12
【问题描述】:
我正在尝试创建一个反转字符串字母大小写的函数,因此字符串“John”将是“jOHN”。
这是我的代码:
const upperLower = function(string){
let newString ="", newChar ="";
for(let i = 0; i < string.length; i++){
if (string.charAt(i) === " "){
newChar = " "
} else if (string.charAt(i) === string.charAt(i).toUpperCase()){
newChar = string.charAt(i).toLowerCase;
} else {
newChar = string.charAt(i).toUpperCase;
}
newString += newChar;
}
return newString;
}
当我使用它时,我得到的是这样的:
"function toLowerCase() { [native code] }function toUpperCase() { [native code] }function toUpperCase() { [native code] }function toUpperCase() { [native code] } function toLowerCase() { [native code] }function toUpperCase() { [native code] }function toUpperCase() { [native code] }function toUpperCase() { [native code] }function toUpperCase() { [native code] }function toLowerCase() { [native code] }"
我哪里出错了,为什么我的结果看起来像这样?谢谢
【问题讨论】:
-
请查看“重复”链接。这不仅仅是同一个任务,你的错误也是相似的。
-
newChar = string.charAt(i).toLowerCase你忘了加上(),因此,你不是调用toLowerCase,而是用函数分配变量newCHar。