Everyone Can Code!

JavaScript Kata #14: Get the Middle Character

By YayoDrayber | Learn To Code With Me | 19 Aug 2022


If you missed the introductory post, it's here. For a list of previously solved katas, please refer to the bottom of this page.

If this is your first time seeing my post, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.

Kata #14

DESCRIPTION:

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"

Kata.getMiddle("middle") should return "dd"

Kata.getMiddle("A") should return "A"

#Input

A word (string) of length 0 < str < 1000 (In javascript you may get slightly more than 1000 in some test cases due to an error in the test cases). You do not need to test for this. This is only here to tell you that you do not need to worry about your solution timing out.

#Output

The middle character(s) of the word represented as a string.

Starting code:

function getMiddle(s)
{
  //Code goes here!
}

My attempt:

function getMiddle(s){
  let sMid = 0.0
  if(s.length % 2 == 0){
    sMid = (s.length/2)
    return s.substring(sMid-1,sMid+1)
  } else{
    sMid = (s.length/2) - 0.5
    return s.substr(sMid,1)
  }
}

Rank #1 'Best Practice':

function getMiddle(s)
{
  return s.substr(Math.ceil(s.length / 2 - 1), s.length % 2 === 0 ? 2 : 1);
}

Should've known there was an easier way to round up with Math.ceil() instead of what I did.

Didn't want to use .slice() on this problem but rank #2 demonstrates how to do it just in case:

function getMiddle(s) {
  var middle = s.length / 2;
  return (s.length % 2) 
    ? s.charAt(Math.floor(middle))
    : s.slice(middle - 1, middle + 1);
}

Rank #3 with a simpler .slice() usage:

function getMiddle(s)
{
  return s.slice((s.length-1)/2, s.length/2+1);
}

Rank #4 with Math.floor()'s round down:

function getMiddle(s)
{
  let middle = Math.floor(s.length/2);
  
  return s.length % 2 === 0
        ? s.slice(middle-1, middle+1)
        : s.slice(middle, middle+1);
  
}

That's it for Kata #14, stay tuned for more katas to be solved!

Link to Kata #1: Square(n) Sum

Link to Kata #2: Convert a Number to a String

Link to Kata #3: DNA to RNA Conversion

Link to Kata #4: Remove First and Last Character

Link to Kata #5: MakeUpperCase

Link to Kata #6: Total amount of points

Link to Kata #7: A Needle in the Haystack

Link to Kata #8: Sum of positive

Link to Kata #9: Basic Mathematical Operations

Link to Kata #10: Beginner - Reduce but Grow

Link to Kata #11: Square Every Digit

Link to Kata #12: Friend or Foe?

Link to Kata #13: Grasshopper - Summation

How do you rate this article?

6


YayoDrayber
YayoDrayber

A Yayo, A Drayber, among others.


Learn To Code With Me
Learn To Code With Me

Just started with Codewars and will be posting the solutions I came up with here and the apparent best practices.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.