From ca0af15a1afcbbdb171372b480c72851dae69727 Mon Sep 17 00:00:00 2001 From: Evan Moore Date: Sat, 26 Mar 2016 20:57:27 -0600 Subject: [PATCH] pass random shift length as second argument --- exercises/caeser_cypher/exercise.js | 8 +++++-- exercises/caeser_cypher/solution/solution.js | 23 ++++++++++---------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/exercises/caeser_cypher/exercise.js b/exercises/caeser_cypher/exercise.js index 4fcbe81..675cebe 100644 --- a/exercises/caeser_cypher/exercise.js +++ b/exercises/caeser_cypher/exercise.js @@ -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) @@ -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) }) diff --git a/exercises/caeser_cypher/solution/solution.js b/exercises/caeser_cypher/solution/solution.js index fc5afa7..72a6c01 100644 --- a/exercises/caeser_cypher/solution/solution.js +++ b/exercises/caeser_cypher/solution/solution.js @@ -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)