Skip to content

Instantly share code, notes, and snippets.

@alesanabriav
Created September 23, 2019 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alesanabriav/a86c06c4c8f5b114ebf38a3056f23c1c to your computer and use it in GitHub Desktop.
Save alesanabriav/a86c06c4c8f5b114ebf38a3056f23c1c to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/wixokok
<!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>
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