JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Tell us what’s happening:
I can’t get into the else statement. Is there something I’m missing?
Your code so far

function rot13(str) {
  function getObjectKey(obj, value) {
  return Object.keys(obj).find((key) => obj[key] === value);
}

  const cipher ={
    "A" : 1,
    "B" : 2,
    "C" : 3,
    "D" : 4,
    "E" : 5,
    "F" : 6,
    "G" : 7,
    "H" : 8,
    "I" : 9,
    "J" : 10,
    "K" : 11,
    "L" : 12,
    "M" : 13,
    "N" : 14,
    "O" : 15,
    "P" : 16,
    "Q" : 17,
    "R" : 18,
    "S" : 19,
    "T" : 20,
    "U" : 21,
    "V" : 22,
    "W" : 23,
    "X" : 24,
    "Y" : 25,
    "Z" : 0
  }
  var counter = 0;
  var strArr = str.split("")

  const res = strArr.reduce((arr, elem) =>{
     if (elem !== "!" || elem !== "?" || elem !== "." || elem !== " "){
       counter = (cipher[elem] + 13) % 26
      let obj = getObjectKey(cipher, counter)
      arr.push(obj)
    }
     else{
       arr.push(elem)
     }

    return arr
  }, [])
  console.log(res)
  const resStr = res.join("")
  console.log(resStr)
  return resStr;
}

rot13("SERR PBQR PNZC");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.34

Challenge: JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Link to the challenge:

ohhhhh I see… I want to say “if we dont have a letter, just push the non-letter character, else use the modulo operator (etc.)” Should Iswitch it to like elem == "!"[...] arr.push(elem) ?

Edit: Yep that did the trick, thank you for your help :smiley:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.