trying to convert a letter to uppercase using .toUpperCase()… doesn't work?

后端 未结 2 1009
轮回少年
轮回少年 2021-01-28 08:47

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?<

2条回答
  •  孤街浪徒
    2021-01-28 09:20

    The above answer is correct, however I slightly modified the code you wrote, as things can be achieved without using arrays.

    var test = "hello*3";
    LetterChanges(test)
    
    function LetterChanges(str) {
    var newStr = ""
     for (i = 0; i < str.length; i++) {
     var ch = str[i];
     if(ch >='A' && ch<='z'){
        ch =  String.fromCharCode(ch.charCodeAt(0) + 1);
       if(ch == 'p' || ch == 'i')
       ch = ch.toUpperCase();
       }
       newStr += ch;
     }
    
    console.log(newStr);
    return newStr;
    }
    

    Here is the fiddle : https://jsfiddle.net/fzme18q0/2/

提交回复
热议问题