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

后端 未结 2 1010
轮回少年
轮回少年 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/

    0 讨论(0)
  • 2021-01-28 09:35

    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();
    
    0 讨论(0)
提交回复
热议问题