将字符串中的奇数和偶数索引字符转换为 Javascript 中的大写/小写? [英] Converting Odd and Even-indexed characters in a string to uppercase/lowercase in Javascript?
本文介绍了将字符串中的奇数和偶数索引字符转换为 Javascript 中的大写/小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一个函数来读取字符串输入并将字符串中的奇数索引字符转换为大写,将偶数索引字符转换为小写.
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 alternativeCase(string){
for(var i = 0; i < string.length; i++){
if (i % 2 != 0) {
string[i].toUpperCase();
}
else {
string[i].toLowerCase();
}
}
return string;
}
如何修复我的代码?
推荐答案
试试这个:
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;
}
这篇关于将字符串中的奇数和偶数索引字符转换为 Javascript 中的大写/小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文