Skip to content

Commit a93d5cf

Browse files
authored
Update keyFinder.js
1. modified the identifiers and used the suitable identifiers for the variables 2. leave the loop when a key is match and found to increase the speed of searching in this stage of development 3. return the key number if founded, return 0 if found nothing
1 parent 6c894a2 commit a93d5cf

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

Ciphers/keyFinder.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ Note: This is a draft version, please help to modify, Thanks!
55
function keyFinder(str){ // str is used to get the input of encrypted string
66
const wordbank =["is","Is","am","Am","are","Are","have","Have","has","Has","may","May","be","Be"];
77
let key = 0; // return zero means the key can not be found
8-
//var shiftNum = 0; //count the number of key shifted
98
let inStr = str.toString(); //convert the input to String
109
let outStr = ""; // store the output value
1110
let wordInOutStr = ""; // temporary store the word inside the outStr, it is used for comparison
12-
//document.getElementById("debug").innerHTML = shiftNum; // debug: display the shifted number(s)
1311
for (let i=0; i<(52); i++){ //try the number of key shifted, the sum of character from a-z and A-Z is 26*2=52
1412
outStr = caesarCipherEncodeAndDecodeEngine(inStr,i); // use the encrytpion engine to decrypt the input string, shiftNum=i
1513
for ( let i=0; i<wordbank.length; i++){
@@ -20,9 +18,9 @@ function keyFinder(str){ // str is used to get the input of encrypted string
2018
// this part need to be optimize with the calculation of the number of occurance of word's probabilities
2119
// linked list will be used in the next stage of development to calculate the number of occurace of the key
2220
if (wordbank[i] == wordInOutStr){
23-
return key=i;
21+
return key=i; // return the key number if founded
2422
}
2523
}
2624
}
27-
return key;
25+
return 0; // return 0 if found nothing
2826
}

0 commit comments

Comments
 (0)