Converting Odd and Even-indexed characters in a string to uppercase/lowercase in Javascript?

后端 未结 4 779
眼角桃花
眼角桃花 2021-01-26 11:12

I need to make a function that reads a string input and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase.

function          


        
4条回答
  •  庸人自扰
    2021-01-26 11:39

    Try this:

    function alternativeCase(string){
        var output = "";
        for(var i = 0; i < string.length; i++){
            if (i % 2 != 0) {
                output += string[i].toUpperCase();
            }
            else {
                output += string[i].toLowerCase();
             }   
        }
        return output;
    }
    

提交回复
热议问题