尝试转换一个字母使用.toUpperCase()...不起作用为大写? [英] trying to convert a letter to uppercase using .toUpperCase()... doesn't work?
问题描述
我想一个字符转换阵列中的使用.toUpperCase()为大写,但它不工作。我是pretty肯定我的逻辑是正确的,所以不知道为什么它不工作?
VAR测试=你好* 3;\r
\r
\r
功能LetterChanges(STR){\r
//变种testArr = str.split();\r
变种tempArr = [];\r
\r
对于(i = 0; I< str.length;我++){\r
如果(str.char $ C $猫(I)GT = 97安培;&安培; str.char $ C $猫(I)LT = 122){\r
\r
tempArr.push(str.char $ C $猫(一));\r
tempArr [I] = tempArr [I] + 1;\r
tempArr [I] = String.fromChar code(tempArr [I]);\r
\r
如果(tempArr由[i] ==P|| tempArr由[i] ==I){\r
tempArr [I] .toUpperCase();\r
的console.log(tempArr [I]); //这将显示i和P。为什么?\r
}\r
\r
}其他{\r
tempArr [I] = STR [I]\r
}\r
\r
\r
}\r
的console.log(tempArr); //这显示['我','F','M','M','P','*','3']\r
\r
海峡= tempArr.join();\r
返回海峡;\r
\r
}
\r
如此看来,当我比较P和i,它能够检测到它,但它并没有将其转换为大写。
任何帮助将是AP preciated。谢谢!
的console.log(tempArr [I]); //这将显示i和P。为什么?
块引用>因为你不存储转换后的值回变量
使其
tempArr [I] = tempArr [I] .toUpperCase();
I'm trying to convert a character in an array to uppercase using .toUpperCase(), but it doesn't work. I'm pretty sure my logic is correct, so not sure why it's not working?
var test = "hello*3"; function LetterChanges(str) { //var testArr = str.split(""); var tempArr = []; for (i = 0; i < str.length; i++) { if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) { tempArr.push(str.charCodeAt(i)); tempArr[i] = tempArr[i] + 1; tempArr[i] = String.fromCharCode(tempArr[i]); if (tempArr[i] == "p" || tempArr[i] == "i") { tempArr[i].toUpperCase(); console.log(tempArr[i]); //this will display "i" and "p".. why? } } else { tempArr[i] = str[i]; } } console.log(tempArr); //this display [ 'i', 'f', 'm', 'm', 'p', '*', '3' ] str = tempArr.join(""); return str; }
So it seems that when I'm comparing "p" and "i" it was able to detect it, but it doesn't convert it to uppercase.
Any help would be appreciated. Thanks!
解决方案console.log(tempArr[i]); //this will display "i" and "p".. why?
because you were not storing the converted value back to the variable
make it
tempArr[i] = tempArr[i].toUpperCase();
这篇关于尝试转换一个字母使用.toUpperCase()...不起作用为大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!