Skip to content

Commit 2e0bd65

Browse files
committed
Fixing non compliant files (Ciphers, Conversions and Data Structures)
1 parent 14bab1b commit 2e0bd65

File tree

8 files changed

+282
-253
lines changed

8 files changed

+282
-253
lines changed

Ciphers/keyFinder.js

Lines changed: 79 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,148 @@
11
/******************************************************
2-
Find and retrieve the encryption key automatically
3-
Note: This is a draft version, please help to modify, Thanks!
4-
******************************************************/
2+
Find and retrieve the encryption key automatically
3+
Note: This is a draft version, please help to modify, Thanks!
4+
******************************************************/
55
function keyFinder (str) { // str is used to get the input of encrypted string
6-
const wordbank = ['I ', 'You ', 'We ', 'They ', 'He ', 'She ', 'It ', ' the ', 'The ', ' of ', ' is ', 'Is ', ' am ', 'Am ', ' are ', 'Are ', ' have ', 'Have ', ' has ', 'Has ', ' may ', 'May ', ' be ', 'Be ']
6+
const wordBank = [
7+
'I ',
8+
'You ',
9+
'We ',
10+
'They ',
11+
'He ',
12+
'She ',
13+
'It ',
14+
' the ',
15+
'The ',
16+
' of ',
17+
' is ',
18+
'Is ',
19+
' am ',
20+
'Am ',
21+
' are ',
22+
'Are ',
23+
' have ',
24+
'Have ',
25+
' has ',
26+
'Has ',
27+
' may ',
28+
'May ',
29+
' be ',
30+
'Be ']
731
// let wordbankelementCounter = 0;
832
// let key = 0; // return zero means the key can not be found
933
const inStr = str.toString() // convert the input to String
1034
let outStr = '' // store the output value
1135
let outStrElement = '' // temporary store the word inside the outStr, it is used for comparison
12-
for (let k = 0; k < 26; k++) { // try the number of key shifted, the sum of character from a-z or A-Z is 26
13-
outStr = caesarCipherEncodeAndDecodeEngine(inStr, k) // use the encrytpion engine to decrypt the input string
36+
for (let k = 0; k < 26; k++) { // try the number of key shifted, the sum of character from a-z or A-Z is 26
37+
outStr = caesarCipherEncodeAndDecodeEngine(inStr, k) // use the encryption engine to decrypt the input string
1438

1539
// loop through the whole input string
1640
for (let s = 0; s < outStr.length; s++) {
17-
for (let i = 0; i < wordbank.length; i++) {
41+
for (let i = 0; i < wordBank.length; i++) {
1842
// initialize the outStrElement which is a temp output string for comparison,
19-
// use a loop to find the next digit of wordbank element and compare with outStr's digit
20-
for (let w = 0; w < wordbank[i].length; w++) {
43+
// use a loop to find the next digit of wordBank element and compare with outStr's digit
44+
for (let w = 0; w < wordBank[i].length; w++) {
2145
outStrElement += outStr[s + w]
2246
}
2347

24-
// console.log( k + outStrElement + wordbank[i] );//debug
48+
// console.log( k + outStrElement + wordBank[i] );//debug
2549

26-
// this part need to be optimize with the calculation of the number of occurance of word's probabilities
50+
// this part need to be optimize with the calculation of the number of occurrence of word's probabilities
2751
// linked list will be used in the next stage of development to calculate the number of occurace of the key
28-
if (wordbank[i] == outStrElement) {
29-
return k // return the key number if founded
52+
if (wordBank[i] === outStrElement) {
53+
return k // return the key number if founded
3054
}
3155

3256
outStrElement = '' // reset the temp word
33-
} // end for ( let i=0; i < wordbank.length; i++)
57+
} // end for ( let i=0; i < wordBank.length; i++)
3458
}
3559
}
3660
return 0 // return 0 if found nothing
3761
}
3862

39-
/* this sub-function is used to assist the keyfinder to find the key */
63+
/* this sub-function is used to assist the keyFinder to find the key */
4064
function caesarCipherEncodeAndDecodeEngine (inStr, numShifted) {
4165
const shiftNum = numShifted
4266
let charCode = 0
4367
let outStr = ''
44-
let shftedcharCode = 0
68+
let shiftedCharCode = 0
4569
let result = 0
4670

4771
for (let i = 0; i < inStr.length; i++) {
4872
charCode = inStr[i].charCodeAt()
49-
shftedcharCode = charCode + shiftNum
73+
shiftedCharCode = charCode + shiftNum
5074
result = charCode
5175

5276
if ((charCode >= 48 && charCode <= 57)) {
53-
if (shftedcharCode < 48) {
54-
let diff = Math.abs(48 - 1 - shftedcharCode) % 10
77+
if (shiftedCharCode < 48) {
78+
let diff = Math.abs(48 - 1 - shiftedCharCode) % 10
5579

5680
while (diff >= 10) {
57-
diff = diff % 10
81+
diff = diff % 10
5882
}
5983
document.getElementById('diffID').innerHTML = diff
6084

61-
shftedcharCode = 57 - diff
85+
shiftedCharCode = 57 - diff
6286

63-
result = shftedcharCode
64-
} else if (shftedcharCode >= 48 && shftedcharCode <= 57) {
65-
result = shftedcharCode
66-
} else if (shftedcharCode > 57) {
67-
let diff = Math.abs(57 + 1 - shftedcharCode) % 10
87+
result = shiftedCharCode
88+
} else if (shiftedCharCode >= 48 && shiftedCharCode <= 57) {
89+
result = shiftedCharCode
90+
} else if (shiftedCharCode > 57) {
91+
let diff = Math.abs(57 + 1 - shiftedCharCode) % 10
6892

6993
while (diff >= 10) {
70-
diff = diff % 10
94+
diff = diff % 10
7195
}
7296
document.getElementById('diffID').innerHTML = diff
7397

74-
shftedcharCode = 48 + diff
98+
shiftedCharCode = 48 + diff
7599

76-
result = shftedcharCode
100+
result = shiftedCharCode
77101
}
78102
} else if ((charCode >= 65 && charCode <= 90)) {
79-
if (shftedcharCode <= 64) {
80-
let diff = Math.abs(65 - 1 - shftedcharCode) % 26
103+
if (shiftedCharCode <= 64) {
104+
let diff = Math.abs(65 - 1 - shiftedCharCode) % 26
81105

82106
while ((diff % 26) >= 26) {
83-
diff = diff % 26
107+
diff = diff % 26
84108
}
85-
shftedcharCode = 90 - diff
86-
result = shftedcharCode
87-
} else if (shftedcharCode >= 65 && shftedcharCode <= 90) {
88-
result = shftedcharCode
89-
} else if (shftedcharCode > 90) {
90-
let diff = Math.abs(shftedcharCode - 1 - 90) % 26
109+
shiftedCharCode = 90 - diff
110+
result = shiftedCharCode
111+
} else if (shiftedCharCode >= 65 && shiftedCharCode <= 90) {
112+
result = shiftedCharCode
113+
} else if (shiftedCharCode > 90) {
114+
let diff = Math.abs(shiftedCharCode - 1 - 90) % 26
91115

92116
while ((diff % 26) >= 26) {
93-
diff = diff % 26
117+
diff = diff % 26
94118
}
95-
shftedcharCode = 65 + diff
96-
result = shftedcharCode
119+
shiftedCharCode = 65 + diff
120+
result = shiftedCharCode
97121
}
98122
} else if ((charCode >= 97 && charCode <= 122)) {
99-
if (shftedcharCode <= 96) {
100-
let diff = Math.abs(97 - 1 - shftedcharCode) % 26
123+
if (shiftedCharCode <= 96) {
124+
let diff = Math.abs(97 - 1 - shiftedCharCode) % 26
101125

102126
while ((diff % 26) >= 26) {
103-
diff = diff % 26
127+
diff = diff % 26
104128
}
105-
shftedcharCode = 122 - diff
106-
result = shftedcharCode
107-
} else if (shftedcharCode >= 97 && shftedcharCode <= 122) {
108-
result = shftedcharCode
109-
} else if (shftedcharCode > 122) {
110-
let diff = Math.abs(shftedcharCode - 1 - 122) % 26
129+
shiftedCharCode = 122 - diff
130+
result = shiftedCharCode
131+
} else if (shiftedCharCode >= 97 && shiftedCharCode <= 122) {
132+
result = shiftedCharCode
133+
} else if (shiftedCharCode > 122) {
134+
let diff = Math.abs(shiftedCharCode - 1 - 122) % 26
111135

112136
while ((diff % 26) >= 26) {
113-
diff = diff % 26
137+
diff = diff % 26
114138
}
115-
shftedcharCode = 97 + diff
116-
result = shftedcharCode
139+
shiftedCharCode = 97 + diff
140+
result = shiftedCharCode
117141
}
118142
}
119143
outStr = outStr + String.fromCharCode(parseInt(result))
120144
}
121145
return outStr
122146
}
147+
148+
console.log('Testing: ' + keyFinder('test')) // returns 0

Ciphers/vigenereCipher.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Check if the Character is letter or not
3-
* @param {String} character - character to check
3+
* @param {String} str - character to check
44
* @return {object} An array with the character or null if isn't a letter
55
*/
66
function isLetter (str) {
@@ -13,10 +13,10 @@ function isLetter (str) {
1313
* @return {Boolean} result of the checking
1414
*/
1515
function isUpperCase (character) {
16-
if (character == character.toUpperCase()) {
16+
if (character === character.toUpperCase()) {
1717
return true
1818
}
19-
if (character == character.toLowerCase()) {
19+
if (character === character.toLowerCase()) {
2020
return false
2121
}
2222
}

Conversions/DecimalToHex.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ function intToHex (num) {
1111
}
1212

1313
function decimalToHex (num) {
14-
const hex_out = []
14+
const hexOut = []
1515
while (num > 15) {
16-
hex_out.push(intToHex(num % 16))
16+
hexOut.push(intToHex(num % 16))
1717
num = Math.floor(num / 16)
1818
}
19-
return intToHex(num) + hex_out.join('')
19+
return intToHex(num) + hexOut.join('')
2020
}
2121

2222
// test cases

Data Structures/Heap/MinPriorityQueue.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// Functions: insert, delete, peek, isEmpty, print, heapSort, sink
1313

1414
class MinPriorityQueue {
15-
// calss the constructor and initializes the capacity
15+
// calls the constructor and initializes the capacity
1616
constructor (c) {
1717
this.heap = []
1818
this.capacity = c
@@ -43,13 +43,12 @@ class MinPriorityQueue {
4343

4444
// returns boolean value whether the heap is empty or not
4545
isEmpty () {
46-
if (this.size == 0) return true
47-
return false
46+
return this.size === 0
4847
}
4948

5049
// returns boolean value whether the heap is full or not
5150
isFull () {
52-
if (this.size == this.capacity) return true
51+
if (this.size === this.capacity) return true
5352
return false
5453
}
5554

@@ -111,7 +110,7 @@ class MinPriorityQueue {
111110
}
112111

113112
// testing
114-
q = new MinPriorityQueue(8)
113+
const q = new MinPriorityQueue(8)
115114

116115
q.insert(5)
117116
q.insert(2)

0 commit comments

Comments
 (0)