javascript how to get middle letters of a string

Solutions on MaxInterview for javascript how to get middle letters of a string by the best coders in the world

showing results for - "javascript how to get middle letters of a string"
Luca
08 Feb 2016
1 function extractMiddle(str) {
2
3        var position;
4        var length;
5
6        if(str.length % 2 == 1) {
7            position = str.length / 2;
8            length = 1;
9        } else {
10            position = str.length / 2 - 1;
11            length = 2;
12        }
13
14        return str.substring(position, position + length)
15    }
16
17    console.log(extractMiddle("handbananna"));