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){
- //Code goes here!
- const newArr = s.split("");
- let middle;
- if(newArr.length % 2 === 0){
- middle = newArr[(newArr.length / 2)- 1] + newArr[(newArr.length / 2)];
- } else {
- middle = newArr[Math.floor(newArr.length / 2)];
- }
- return middle;
- }
- getMiddle("test")
- // getMiddle("testing")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement