Caesars Cipher - Code works but fails test

Tell us what’s happening:

Your code so far


function rot13(str) { // LBH QVQ VG!
    let codes = {
        N : 'A',
        O : 'B',
        P : 'C',
        Q : 'D',
        R : 'E',
        S : 'F',
        T : 'G',
        U : 'H',
        V : 'I',
        W : 'J',
        X : 'K',
        Y : 'L',
        Z : 'M',
        A : 'N',
        B : 'O',
        C : 'P',
        D : 'Q', 
        E : 'R',
        F : 'S',
        G : 'T',
        H : 'U',
        I : 'V',
        J : 'W',
        K : 'X',
        L : 'Y',
        M : 'Z'
    }
    var newArr = str.split("")
    var returnedArr = []
    for(var i=0; i<newArr.length;i++){
        if(/\W/.test(newArr[i])){
            returnedArr.push(newArr[i])
        } else {
            returnedArr.push(Object.values(codes[newArr[i]]))
        }
    }
   return returnedArr.join("").trim())
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher

  1. Remember to return the string.

  2. You have spaces in the string.

join(" ") should be join("")

1 Like