-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 19 - AlphabetSubsequence.js
75 lines (55 loc) · 1.77 KB
/
Day 19 - AlphabetSubsequence.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
72
73
74
75
/*Alphabet Subsequence
https://scrimba.com/scrim/coe1d4d189ecb6381a1e4edd2
Check whether the given string is a subsequence of the plaintext alphabet.
Example:
For s = "effg" or s = "cdce", the output should be the alphabetSubsequence(s) = false
For s = "ace" or s = "bxz", the output should be alphabetSubsequence(s) = true
Hints: size property(), charCodeAt(), split()
*/
function alphabetSubsequence(str) {
for (i=0; i < str.length - 1; i++) {
let currentElement = str.charCodeAt(i)
let nextElement = str.charCodeAt(i+1)
// console.log("current " + str[i] + "=" + currentElement)
// console.log("next " + str[i+1] + "=" + nextElement)
if (!(nextElement > currentElement)) {
return false
}
}
return true
}
/**
* Test Suite
*/
describe('alphabetSubsequence()', () => {
it('returns false when it has duplicate letters', () => {
// arrange
const str = 'effg';
// act
const result = alphabetSubsequence(str);
// log
console.log("result 1: ", result);
// assert
expect(result).toBe(false);
});
it('returns false when NOT in ascending a - z order', () => {
// arrange
const str = 'cdce';
// act
const result = alphabetSubsequence(str);
// log
console.log("result 2: ", result);
// assert
expect(result).toBe(false);
});
it('returns true when no duplicates and is ascending a - z order ', () => {
// arrange
const str = 'ace';
// act
const result = alphabetSubsequence(str);
// log
console.log("result 3: ", result);
// assert
expect(result).toBe(true);
});
});