Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.
- // #Examples:
- // Kata.getMiddle("test") should return "es"
- // Kata.getMiddle("testing") should return "t"
- //split into arr... then is arr.length %2 === 0 then return arr.length / 2 and (next position up)
- //if arr.length %2 !== 0 then return arr.length /2 (round up ... math.ceil)
- function getMiddle(s) {
- var middle = s.length / 2;
- return (s.length % 2)
- ? s.charAt(Math.floor(middle))
- : s.slice(middle - 1, middle + 1);
- }
- // getMiddle("test")
- getMiddle("testing")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement