return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>Converting Odd and Even-indexed characters in a string to uppercaselowercase in Javascript? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Converting Odd and Even-indexed characters in a string to uppercaselowercase in Javascript? - Stack Overflow

programmeradmin3浏览0评论

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;
}

How to fix my code?

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;
}

How to fix my code?

Share Improve this question edited Jan 8, 2019 at 3:55 Cœur 38.8k25 gold badges205 silver badges277 bronze badges asked Jan 3, 2017 at 21:42 Ann0nymAnn0nym 131 silver badge5 bronze badges 1
  • 1 Possible duplicate of Are JavaScript strings immutable? Do I need a "string builder" in JavaScript? – PM 77-1 Commented Jan 3, 2017 at 21:45
Add a ment  | 

4 Answers 4

Reset to default 5
function alternativeCase(string){
  return string.split('').map(function(c,i) {
    return i & 1 ? c.toUpperCase() : c.toLowerCase();
  }).join('');
}

Update 2019

These days it's pretty safe to use ES6 syntax:

const alternativeCase = string => string.split('')
  .map((c,i) => i & 1 ? c.toUpperCase() : c.toLowerCase()).join('');

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;
}

Strings in JavaScript are immutable, Try this instead:

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

RegExp alternative that handles space between characters :

const alternativeCase = s => s.replace(/(\S\s*)(\S?)/g, (m, a, b) => a.toUpperCase() + b.toLowerCase());

console.log( alternativeCase('alternative Case') )

发布评论

评论列表(0)

  1. 暂无评论