-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.validSuffix.js
More file actions
42 lines (38 loc) · 1.07 KB
/
string.validSuffix.js
File metadata and controls
42 lines (38 loc) · 1.07 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
/*
* @title: validate suffix exits in word
* @description: //
* @author: Thorsten Kober
* @email: [email protected]
*/
function confirmEnding1(string, target) {
if (string.substr(-target.length) === target) {
return true;
}
return false;
}
function confirmEnding2(string, target) {
let index1 = string.length - 1;
let index2 = target.length - 1;
while (index1 >= 0 && index2 >= 0) {
if (string[index1] === target[index2]) {
index1--;
index2--;
} else {
return false;
}
}
return true;
}
// npx jest algorithms/string/string.validSuffix.js
describe('validate suffix of word', () => {
test('confirmEnding1()', () => {
expect(confirmEnding1('Bastian', 'n')).toBe(true);
expect(confirmEnding1('Open sesame', 'same')).toBe(true);
expect(confirmEnding1('Open sesame', 'pen')).toBe(false);
});
test('confirmEnding2()', () => {
expect(confirmEnding2('Bastian', 'n')).toBe(true);
expect(confirmEnding2('Open sesame', 'same')).toBe(true);
expect(confirmEnding2('Open sesame', 'pen')).toBe(false);
});
});