Search code examples
javascriptencryptionstring-formattingcaesar-cipher

Remove commas and make it a string?


function Cipher(str) { 
    var strArr = [];
    for(i=0;i<str.length+1;i++){
        var unicode = str.charCodeAt(i);
        var ciphUnicode;
        var newStr = String.fromCharCode(ciphUnicode);

        if(unicode>=65 && unicode <=77 )
            ciphUnicode = unicode + 13;
        else if (unicode>=78 && unicode <=90 && unicode!==" ")
           ciphUnicode = unicode - 13;
        else if (unicode === 32)
            ciphUnicode = unicode;
        strArr.push(newStr);
    }
    return strArr.toString();
}

Cipher("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.") should decode to "THE QUICK BROWN DOG JUMPED OVER THE LAZY FOX." but instead I get "T,H,E, ,Q,U,I,C,K, ,B,R,O,W,N, ,D,O,G, ,J,U,M,P,E,D, ,O,V,E,R, ,T,H,E, ,L,A,Z,Y, ,F,O,X,X"


Solution

  • Try changing strArr.toString() to strArr.join('').

    This joins the strings in the array into a single string. The empty string inside join('') ensures there's no commas - the items are each separated by an empty string instead.