-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanswer.js
72 lines (63 loc) · 1.8 KB
/
answer.js
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
61
62
63
64
65
66
67
68
69
70
71
const m1 = 111;
const m2 = 12345;
const m3 = 4;
// Less optimized, clearer version
const decode = (message) => {
message = message.toString();
const split1 = [];
const split2 = [];
//Split1, start at index 0
for (var i = 0; i < message.length; i+=2) {
// If we have enough letters left to make a par
if(message.charAt(i+1)){
split1.push(message.charAt(i) + message.charAt(i+1));
} else {
split1.push(message.charAt(i));
}
}
//Split1, start at index 1
split2.push(message.charAt(0));
for (var i = 1; i < message.length; i+=2) {
// If we have enough letters left to make a par
if(message.charAt(i+1)){
split2.push(message.charAt(i) + message.charAt(i+1))
} else {
split2.push(message.charAt(i))
}
}
console.log(split1, split2)
// base : one way to solve
// Every time we encounter an index that is <= 26, that's another way
const nbWays = 1 + split1.concat(split2).filter( x => x <= 26 && x >= 10).length ;
return nbWays
}
// Optimized, more cryptic version
const decode2 = (message) => {
message = message.toString();
const split1 = [];
const split2 = [];
split2.push(message.charAt(0));
for (var i = 0; i < message.length; i++) {
if(i % 2 === 0) {
if(message.charAt(i+1)){
split1.push(message.charAt(i) + message.charAt(i+1));
} else {
split1.push(message.charAt(i));
}
} else {
if(message.charAt(i+1)){
split2.push(message.charAt(i) + message.charAt(i+1))
} else {
split2.push(message.charAt(i))
}
}
}
console.log(split1, split2)
const nbWays = 1 + split1.concat(split2).filter( x => x <= 26 && x >= 10).length ;
return nbWays
}
/*
console.log(decode(m2));
console.log('---------')
*/
console.log(decode2(m2));