gpt4 book ai didi

javascript - 尝试使用 .toUpperCase() 将字母转换为大写...不起作用?

转载 作者:行者123 更新时间:2023-11-28 18:43:19 25 4
gpt4 key购买 nike

我尝试使用 .toUpperCase() 将数组中的字符转换为大写,但它不起作用。我很确定我的逻辑是正确的,所以不确定为什么它不起作用?

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;

}

看来,当我比较“p”和“i”时,它能够检测到它,但它不会将其转换为大写。

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

console.log(tempArr[i]); //this will display "i" and "p".. why?

因为您没有将转换后的值存储回变量

成功

tempArr[i] = tempArr[i].toUpperCase();

关于javascript - 尝试使用 .toUpperCase() 将字母转换为大写...不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35790177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com