-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-the-middle-character.js
More file actions
46 lines (33 loc) · 1.25 KB
/
Get-the-middle-character.js
File metadata and controls
46 lines (33 loc) · 1.25 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
//7 Kyu
//Get the middle character
//Fundamentals, strings
// You are going to be given a non-empty string. Your job is to return the middle character(s) of the string.
// If the string's length is odd, return the middle character.
// If the string's length is even, return the middle 2 characters.
// Examples:
// "test" --> "es"
// "testing" --> "t"
// "middle" --> "dd"
// "A" --> "A"
//Solution I
function getMiddle(s) {
//store string length
let length = s.length
//check if the length is a perfect factor of 2, if so slice to grab the middle chracter
if(length % 2 === 0){
return s.slice((length/2)-1, (length/2)+1)
//if the length is not a perfect factor of 2 , if not slice for the middle letter
//note when using slice, if the integer has a fraction it will be trunkated. ex. 7/2=3.5 but slice will trunk it down to 3.
}else if(length%2 !== 0){
return s.slice((length/2),(length/2)+1)
}
}
//Parameters
//no empty strings, strings with even length and different length will have a different return
//no symbols, no funny bizz
//Results
//if str.length % 2 == 0 -> return middle 2 character
//if str.length % 2 != 0 -> return middle character
//Examples
//console.log(getMiddle("test"),es)
//console.log(getMiddle("testing")t)