-2

You may be familiar with caeser cipher(see this wiki on caesers cipher AKA ROT13)

I have been looking at solutions for the cipher but I do not see anyone using a switch statement. Given the challenge (see below), can this problem be simply solved by using a switch statement? I have put my code below but it does not work.

function cciph(str) { 
  var code = "";

  switch {switch (str) {
    case 'A':
      code = "N";
      break;
    case 'B:
      code = 'O';
      break;
      //.....
    case 'M':  code = 'Z';
  }
  return str; 
}

cciph("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");
Dr.Apell
  • 131
  • 9
  • 3
    Seems like your question is missing some information about your current attempt to solve the problem. Did you miss a block of code? – Ken May 16 '17 at 15:11
  • No. the challenge i written that way. you can change it if you like. You do not have to follow the format but the cipher remains . Hope it helps – Dr.Apell May 16 '17 at 15:16
  • 2
    You have a function called `cciph` that accept a parameter and return it as-is. If this is what you were trying to do then your code works well – Alon Eitan May 16 '17 at 15:17
  • 2
    "No. the challenge i written that way." I believe that what @Ken was trying to tell you is "this is not a free write-your-code-for-you / do-your-homework-for-you service." – Daniel Beck May 16 '17 at 15:21
  • 1
    Possible duplicate of [Where is my one-line implementation of rot13 in JavaScript going wrong?](http://stackoverflow.com/questions/617647/where-is-my-one-line-implementation-of-rot13-in-javascript-going-wrong) – gforce301 May 16 '17 at 15:29
  • @DanielBeck The question I am asking primarily is if this can be written using a switch statement. I have seen many solutions which are good but I am curious as to why no one uses a switch. As i mentioned in my question I am learning. I thought that is the spirit of Stack overflow ..if i was looking for a "....a free write-your-code-for-you / do-your-homework-for-you service." I coudl have copied any of the thousands of codes already availble online ...so I am very bothered by your insinuation – Dr.Apell May 16 '17 at 15:45
  • Your switch solution would work for a single case. If you had a coded string and you were trying to break the cipher then your switch would only work 1/26th of the time. – Ken May 16 '17 at 15:55
  • @Dr.Apell No one uses a switch because it's clunky and static. It can be done, though. https://repl.it/IBGG/0 – mhodges May 16 '17 at 15:58
  • Your question is much improved after the edit and now more or less fits within the rules here https://stackoverflow.com/help/on-topic so I'm removing my downvote. (n.b. the overall strategy in your code would work fine if you modified it to iterate through each character of the input string.) – Daniel Beck May 16 '17 at 16:01
  • @ Daniel Much Obliged – Dr.Apell May 16 '17 at 16:34

1 Answers1

0

I don't know about a switch, but this will give you all 26 possible mutations of the string, theoretically including the decoded string.

var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];


function cciph(cipherText) {
  for(i = 0; i < 26; i++) {
    var decodedText = cipherText.split("").map(function(c) {
      var charIndex = alphabet.indexOf(c.toLowerCase());
      if (charIndex == -1) {
        return c;
      }
      var modifiedIndex = charIndex + i;
      var correctedIndex = modifiedIndex % 26;
      return alphabet[correctedIndex];
    });
    console.log(decodedText.join(""));
  }
}

cciph("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");
Ken
  • 466
  • 2
  • 7
  • Thank you @ Ken. Look at the edited question above. I have tried to use a switch but it does not work. is it possible to tweak it ? Or the switch code is not viable? – Dr.Apell May 16 '17 at 15:58
  • I responded above. Essentially your switch would be mapping the characters of the alphabet to exactly one other outcome. You would need to mutate the output of each letter's case based on some input like an iterator inside of a loop so that you can test all 26 possibilities. @Dr.Apell – Ken May 16 '17 at 16:01
  • @ Ken. Thanks for the insight. – Dr.Apell May 16 '17 at 16:35