本帖最后由 jnseo66 于 2014-09-09 10:16:18 编辑

解决方案 »

  1.   

    20分太少了 function encode(oriStr){
    var result="";
    l=oriStr.length; // 长度
    a=b=Math.round(l/2); // 长度的"一半" if(l<2*a)
    b=b-1; for(i=0;i<b;i++) // 遍历整个字符串
    result = result + oriStr.charAt(i)+ oriStr.charAt(i+b); if(l<2*a) 
    result = result + oriStr.charAt(l-1);

    document.writeln(oriStr + ", len: " + b +" => " + result + " => " + decode(result) +  "<br/>");
    return result;
    } function decode(oriStr){
    var len = oriStr.length, result = new Array(oriStr.length), offset = parseInt(len / 2), times = 0;

    for(i=0;i<len; i++){
    if(i == len - 1 && len % 2 != 0)
    result[len - 1] = oriStr.charAt(i);
    else{
    if(i % 2 == 0){
    result[times] = oriStr.charAt(i);
    }else{
    result[times + offset] = oriStr.charAt(i);
    times++;
    }
    }
    } return result.join("");
    } encode("abcdefg")
    encode("abcdefgh")
    encode("abcdefghi")
    encode("abcdefghij")
      

  2.   

    function encode(inp){
      var r1 = r2 = '';
      l = inp.length; // 长度
      len = Math.floor(l/2); // 长度的"一半"
     
      for(i=0; i<len*2; i+=2) {
        r1 += inp.substr(i, 1);
        r2 += inp.substr(i+1, 1)
      }
      r1 += r2;
      if(l > 2*len) r1 += inp.substr(l-1, 1);
      return r1;
    }
      

  3.   

    function Encode(str) {
        var a = b = "";
        
        for(var j = 0;j < (str.length/2|0) *2;j += 2){
            a += str.charAt(j);
            b += str.charAt(j+1);
        }
        return a + b + (str.length % 2 ? str[str.length-1]: "");
    }