Created
September 18, 2013 15:06
-
-
Save cangevine/6610515 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Caesar Cipher</title> | |
| <script> | |
| function cipher() { | |
| var alphabet = "abcdefghijklmnopqrstuvwxyz"; | |
| var secretMessage = "alec"; | |
| var key = 3; | |
| for(i = 0; i < secretMessage.length; i++){ | |
| var letterNumber = secretMessage.charAt(i); | |
| var indexFinder = alphabet.indexOf(letterNumber); | |
| var newIndex = indexFinder + key; | |
| if(newIndex > 25){ | |
| newIndex = newIndex - 25; | |
| }else{ | |
| var newLetter = secretMessage.charAt(newIndex); | |
| return newLetter; | |
| } | |
| return newLetter; | |
| } | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <div id = thisIsWhereChampionsAreMade> | |
| <button onClick = "cipher()">Press Here to Cipher Secret Message</button> | |
| </div> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#11: Not an error, but "number" in the variable name may be misleading. What is this variable actually storing?
#14: Nicely done!
#16: Here's a problem: you have already done the "wrap around" at this point. You should now be able to look up the newLetter based on newIndex -- this is only looking up newLetter if a wrap around was not needed.
#18 & 20: Remember that a return statement will always return a value and END the method. The problem here is that it will return newLetter before the for() loop has finished.
---- Your code is on the right path, but I am unable to test this in the browser. Were you able to test this successfully?