Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. //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.
  2.  
  3. // #Examples:
  4.  
  5. // Kata.getMiddle("test") should return "es"
  6.  
  7. // Kata.getMiddle("testing") should return "t"
  8.  
  9. //split into arr... then is arr.length %2 === 0 then return arr.length / 2 and (next position up)
  10.  
  11. //if arr.length %2 !== 0 then return arr.length /2 (round up ... math.ceil)
  12.  
  13. function getMiddle(s) {
  14. var middle = s.length / 2;
  15. return (s.length % 2)
  16. ? s.charAt(Math.floor(middle))
  17. : s.slice(middle - 1, middle + 1);
  18. }
  19.  
  20. // getMiddle("test")
  21. getMiddle("testing")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement