-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperform-string-shift.js
More file actions
41 lines (31 loc) · 959 Bytes
/
perform-string-shift.js
File metadata and controls
41 lines (31 loc) · 959 Bytes
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
/**
* @param {string} s
* @param {number[][]} shift
* @return {string}
*/
var stringShift = function(s, shift) {
var total_shift = 0;
for (let i = 0; i < shift.length; i++) {
var direction = shift[i][0];
var amount = shift[i][1];
if (direction == 0) {
total_shift -= amount;
} else {
total_shift += amount;
}
}
var new_front = "";
var new_back = "";
if (total_shift < 0) {
total_shift = Math.abs(total_shift) % s.length;
new_front = s.substr(total_shift);
new_back = s.substr(0, total_shift);
} else if (total_shift > 0) {
total_shift = total_shift % s.length;
new_front = s.substr(s.length - total_shift, total_shift );
new_back = s.substr(0, s.length - total_shift);
} else {
return s;
}
return new_front + new_back;
};