Created
September 23, 2019 03:54
-
-
Save alesanabriav/a86c06c4c8f5b114ebf38a3056f23c1c to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/wixokok
This file contains 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> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function ceaserCipher(word, num) { | |
num = num % 26; | |
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); | |
const alphabetTotal = alphabet.length; | |
const lc = word.toLowerCase(); | |
let newWord = ''; | |
for(var i = 0; i < lc.length; i++) { | |
let l = lc[i]; | |
let index = alphabet.indexOf(l); | |
let newIndex = index + num; | |
if(l === ' ') { | |
newWord += l; | |
continue; | |
} | |
if(newIndex > alphabetTotal - 1) { | |
newIndex = newIndex - alphabetTotal; | |
} | |
if(newIndex < 0) { | |
newIndex = alphabetTotal + newIndex; | |
} | |
console.log(`index: ${newIndex} ${l} ${index}`); | |
newWord += alphabet[newIndex]; | |
} | |
return newWord; | |
} | |
const n = ceaserCipher("Big Car", -16); | |
console.log('newword', n); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
function ceaserCipher(word, num) { | |
num = num % 26; | |
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); | |
const alphabetTotal = alphabet.length; | |
const lc = word.toLowerCase(); | |
let newWord = ''; | |
for(var i = 0; i < lc.length; i++) { | |
let l = lc[i]; | |
let index = alphabet.indexOf(l); | |
let newIndex = index + num; | |
if(l === ' ') { | |
newWord += l; | |
continue; | |
} | |
if(newIndex > alphabetTotal - 1) { | |
newIndex = newIndex - alphabetTotal; | |
} | |
if(newIndex < 0) { | |
newIndex = alphabetTotal + newIndex; | |
} | |
console.log(`index: ${newIndex} ${l} ${index}`); | |
newWord += alphabet[newIndex]; | |
} | |
return newWord; | |
} | |
const n = ceaserCipher("Big Car", -16); | |
console.log('newword', n);</script></body> | |
</html> |
This file contains 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
function ceaserCipher(word, num) { | |
num = num % 26; | |
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); | |
const alphabetTotal = alphabet.length; | |
const lc = word.toLowerCase(); | |
let newWord = ''; | |
for(var i = 0; i < lc.length; i++) { | |
let l = lc[i]; | |
let index = alphabet.indexOf(l); | |
let newIndex = index + num; | |
if(l === ' ') { | |
newWord += l; | |
continue; | |
} | |
if(newIndex > alphabetTotal - 1) { | |
newIndex = newIndex - alphabetTotal; | |
} | |
if(newIndex < 0) { | |
newIndex = alphabetTotal + newIndex; | |
} | |
console.log(`index: ${newIndex} ${l} ${index}`); | |
newWord += alphabet[newIndex]; | |
} | |
return newWord; | |
} | |
const n = ceaserCipher("Big Car", -16); | |
console.log('newword', n); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment