-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.js
More file actions
60 lines (59 loc) · 1.1 KB
/
cipher.js
File metadata and controls
60 lines (59 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const solution = (word, cipher) => {
// Type your solution here
//split the letters in the cipher
let cipherArray = cipher.split("");
let alphabet = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
];
let cipher_alpha = [];
let wordArray = word.split("");
//create a variable to store decoded word
let solvedArray = [];
let solved = "";
//loop through and decipher the alphabet
for (let i in wordArray) {
if (typeof wordArray[i] == "number") {
solved = "";
}
//if there are duplicates in cipher or space in word then return empty string
if (cipherArray[i] === cipherArray[i - 1]) {
solved = "";
}
if (wordArray[i] == "") {
solved = "";
} else {
for (let j = 0; j < alphabet.length; j++) {
if (wordArray[i] == alphabet[j]) {
solvedArray.push(cipherArray[i]);
solved = solvedArray.join("");
}
}
}
}
return solved;
};