-
Notifications
You must be signed in to change notification settings - Fork 1
/
JavaScript - do_you_speak_retsec.txt
36 lines (25 loc) · 1.12 KB
/
JavaScript - do_you_speak_retsec.txt
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
You're given a single word. Your task is to swap the halves. If the word has an uneven length, leave the character in the middle at that position and swap the chunks around it.
- - - - - - my solution - - - - - - -
function reverseByCenter(s){
var half = Math.floor(s.length/2); <- - - alt: var half = ~~(s.length/2)
var first_half = s.slice(0, half);
if (s.length%2===0) {
var last_half = s.slice(half);
return (last_half + first_half);
} else {
var last_half = s.slice(half+1);
return (last_half + s[half] + first_half);
}
}
- find the half-way point of the string. (the double tilde is shorthand for Math.floor(). good to know.)
- if the string is even
define the last half and return last_half + first_half
- else if the string is odd
define the last half (omit the center char), return last_half + center char + first_half
- - - alt - - -
this is just the slimmed down version of mine.
much sleeker. no need for the if/else statement.
function reverseByCenter(s){
var l = ~~(s.length / 2);
return s.slice(-l) + s.slice(l, l + (s.length % 2)) + s.slice(0, l);
}