ShiftCode --- Computer Science --- Haas

Write a JavaScript program to impliment Caesar's cipher.

In cryptography the shift code, or Caesar cipher, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the text is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method was used by Julius Caesar to communicate with his generals.

The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes today.

Example:

Computer >> Enter a sentence:

User>> I LIKE PIZZA

Computer>> Enter the shift distance:

User>> 4

Computer>>
Sentence: I LIKE PIZZA
Shift distance: 4
Result: M PMOI TMDDE

Below is some code to get you started.



<HTML>
<head>
<title>Shift Code </title>
</head>
<body>
<script language = "JavaScript">
/******************************************************************** 
  Complete the unfinished program below.
********************************************************************/ 

// Here are some variables you might need //
var shifted = "";
var letter;
var position;
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sentence = prompt("Enter sentence:","I LIKE PIZZA").toUpperCase();
var shiftBy = parseInt(prompt("Shift by ","4"));




// <<< Complete the missing code >>>




/*** The following prints out the results ***/
document.write("<br>Sentence: " + sentence);   
document.write("<br>Shift distance: " + shiftBy); 
document.write("<br>Result: " + shifted);  
</script> 
</body>
</HTML>