Skip to content

Instantly share code, notes, and snippets.

@cangevine
Created September 18, 2013 15:27
Show Gist options
  • Save cangevine/6610872 to your computer and use it in GitHub Desktop.
Save cangevine/6610872 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Encrypt</title>
<link type="text/css" rel="stylesheet" href="ceasarcss.css" />
<script>
function encryptText(){
var key = 2;
var msg = document.getElementById('a').value;
for(i=0; i<msg.length; i++){
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var letterNum = msg.charAt(i);
var indexFinder = alphabet.indexOf(letterNum);
var finalIndex = IndexFinder + key;
if(finalIndex > 25){
finalIndex = finalIndex - 25;
var newLetter = msg.charAt(finalIndex);
}else{
var newLetter = msg.charAt(finalIndex);
}
}
var output = document.getElementById('b');
}
</script>
</head>
<body>
<h2 id="in">INPUT:</h2>
<input type = "text" id="a"/>
</br>
<button onClick="encryptText();">ENCRYPT</button>
<button>DECRYPT</button></br>
<h2>OUTPUT:</h2>
<input type = "text" id="b"/>
</body>
</html>
@cangevine
Copy link
Author

#14: Your code looks great up to this point. (And the CSS looks great too!) But at this line the code breaks. Can you identify why? Were you able to test this in Chrome and have it work?
#17-20: This works, but can be simplified. Notice that you repeat the same code on lines 17 and 19. How can you simplify this code and remove the (unnecessary) "else" at the same time?
#21: Now that you have a variable for the newLetter at the end of each time the program loops, what will you do with it? You are doing a great job, but newLetter is not used and thus you cannot display the cipher text with this method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment