-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_ends_with.js
More file actions
68 lines (53 loc) · 1.81 KB
/
12_ends_with.js
File metadata and controls
68 lines (53 loc) · 1.81 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
61
62
63
64
65
66
67
68
/*
Write a function that tells if a string ends with a specific substring
Examples:
endsWith('hello world', 'ld') => true
endsWith('hello world', 'wor') => false
endsWith('hello world', 'hello') => false
**Your function must return a value**
It's not necessary to print the result on screen,
however to test your function you are free to print the result
*/
function getSubString(string, startIndex, endIndex) {
let newString = "";
while (startIndex < endIndex) {
newString += string[startIndex];
startIndex += 1;
}
return newString;
}
function endsWith(string, substring) {
if (substring.length < 1) {
return true;
}
const starIndex = string.length - substring.length;
const endIndex = string.length;
const subStringCandidate = getSubString(string, starIndex, endIndex);
return subStringCandidate === substring;
}
function getMark(isPassed) {
return isPassed ? '✅' : '❌';
}
function getMessage(string, subString, expected, actual) {
let message = "Do '" + subString + "' occur at end of '" + string;
message += "' expected: " + expected + " and is => " + actual;
return message;
}
function testStringEndsWith(string, subString, expected) {
const actual = endsWith(string, subString);
const isPassed = actual === expected;
console.log(getMark(isPassed));
console.log(getMessage(string, subString, expected, actual));
}
function tests() {
testStringEndsWith('a', 'a', true);
testStringEndsWith('a', 'o', false);
testStringEndsWith('a', ' ', false);
testStringEndsWith('a', '', true);
testStringEndsWith('hello world', 'ld', true);
testStringEndsWith('hello world', 'wor', false);
testStringEndsWith('hello world', 'world', true);
testStringEndsWith('hello world.', 'world', false);
testStringEndsWith('hello world', 'hello', false);
}
tests();