-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_string_slice.js
More file actions
76 lines (60 loc) · 1.71 KB
/
04_string_slice.js
File metadata and controls
76 lines (60 loc) · 1.71 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
69
70
71
72
73
74
75
76
/*
Implement the below function that
creates a slice/substring using start and end indices
Examples:
slice('hello world', 0, 4) => 'hello'
slice('negative start', -1, 8) => 'negative '
slice('', 0, 10) => ''
**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 getString(string, start, end) {
let newString = "";
for (let index = start; index <= end; index++) {
newString += string[index];
}
return newString;
}
function max(num1, num2) {
if (num1 < num2) {
return num2;
}
return num1;
}
function min(num1, num2) {
if (num1 < num2) {
return num1;
}
return num2;
}
function slice(text, start, end) {
const givenString = text;
const startIndex = max(start, 0);
const endIndex = min(end, givenString.length - 1);
let subString = getString(givenString, startIndex, endIndex);
return subString;
}
function getMark(isPassed) {
return isPassed ? '✅' : '❌';
}
function getMessage(string, start, end, actual) {
let message = "subString between index '" + start + "-" + end + "' ";
message += "In String '" + string + "' => ";
return message + actual;
}
function testSlice(string, startIndex, endIndex, expected) {
const actual = slice(string, startIndex, endIndex);
const isPassed = actual === expected;
const message = getMessage(string, startIndex, endIndex, actual);
console.log(getMark(isPassed), message);
}
function test() {
testSlice('a', 0, 0, 'a');
testSlice('a', 0, 10, 'a');
testSlice('a', -9, 10, 'a');
testSlice('hello world', 0, 4, 'hello');
testSlice('negative start', -1, 8, 'negative ');
testSlice('', 0, 10, '');
}
test();