Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions exercises/caeser_cypher/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ function getRandomIndex(arr) {
return Math.floor(Math.random() * arr.length)
}

function getRandomShiftLength() {
return Math.floor(Math.random() * (25 - -25 + 1)) + -25
}

// checks that the submission file actually exists
exercise = filecheck(exercise)

Expand All @@ -22,8 +26,8 @@ exercise.addSetup(function(mode, callback){
"Never eat glue.",
"Dogs are the best."
]
var arg = strings[getRandomIndex(strings)]
this.submissionArgs = this.solutionArgs = arg
var args = [strings[getRandomIndex(strings)], getRandomShiftLength()]
this.submissionArgs = this.solutionArgs = args
process.nextTick(callback)
})

Expand Down
23 changes: 12 additions & 11 deletions exercises/caeser_cypher/solution/solution.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
var arg = process.argv[2];
var text = process.argv[2]
var shift = parseInt(process.argv[3])

function caesarCypher(text, shift) {
if (shift < 0)
return caeserShift(str, shift + 26);
return caesarCypher(text, shift + 26)

var output = '';
var output = ''

for (var i = 0; i < text.length; i++) {
var char = text[i];
var char = text[i]

if (char.match(/[a-z]/i)) {
var code = text.charCodeAt(i);
var code = text.charCodeAt(i)

if ((code >= 65) && (code <= 90)) // Uppercase
char = String.fromCharCode(((code - 65 + shift) % 26) + 65);
char = String.fromCharCode(((code - 65 + shift) % 26) + 65)

else if ((code >= 97) && (code <= 122)) // Lowercase
char = String.fromCharCode(((code - 97 + shift) % 26) + 97);
char = String.fromCharCode(((code - 97 + shift) % 26) + 97)
}

output += char;
output += char
}
return output;
return output
}

var result = caesarCypher(arg, 4);
console.log(result);
var result = caesarCypher(text, shift)
console.log(result)