DEV Community

G33kNoob
G33kNoob

Posted on

1

My first codewar solved

the Question :
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.

my solving :
function getMiddle(s) {
var length = s.length;
var middle = length / 2;
let floor = Math.floor(middle);
const middleNumber = Number(middle);
const floorNumber = Number(floor);
const mathNumber = middleNumber - floorNumber;
if (mathNumber > 0) {
let finalMiddle = s[floorNumber];
return finalMiddle;
} else {
let finalMiddle1 = s[floorNumber - 1];
let finalMiddle2 = s[floorNumber];
return finalMiddle1 + finalMiddle2;
}
}

ofcourse i know my solving is not to the point but im really happy to solve this

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay