-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_is_substring.js
More file actions
67 lines (52 loc) · 1.7 KB
/
06_is_substring.js
File metadata and controls
67 lines (52 loc) · 1.7 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
/*
Implement the below function that tells if a string is substring of another string
Usage:
isSubstring('hello world', 'worl') => true
isSubstring('repeating iiiiiiii', 'iii') => true
isSubstring('not found', 'for') => 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++;
}
return newString;
}
function isSubstring(string, subString) {
if (subString.length > 0) {
for (let index = 0; index < string.length; index ++) {
const endIndex = subString.length + index;
const newSubString = getSubString(string, index, endIndex);
if (newSubString === subString) {
return true;
}
}
}
return false;
}
function getMark(isPassed) {
return isPassed ? '✅' : '❌';
}
function getMessage(text, target, expected, actual) {
let message = "Do subString '" + target + "' exists in '" + text + "' ?";
message += " Expexted : " + expected + " and is => " + actual;
return message;
}
function testIsSubStrirng(text, target, expected) {
const actual = isSubstring(text, target);
const isPassed = actual === expected;
console.log(getMark(isPassed), getMessage(text, target, expected, actual));
}
function tests() {
testIsSubStrirng('a', 'a', true);
testIsSubStrirng('a', 'b', false);
testIsSubStrirng('a', '', false);
testIsSubStrirng('hello world', 'worl', true);
testIsSubStrirng('repeating iiiiiiii', 'iii', true);
testIsSubStrirng('not found', 'for', false);
}
tests();