vuyisile.github.io

View on GitHub
8 November 2017

Solving Return Middle Char

by {"name"=>"Vuyisile Weni"}

Finding the middle Charactor

In a task given to me by free code camp to find the middle charecter of a string, I had to create an algorithm that would solve the challenge given at hand.

creating the algorithm

Situation 1:

First i declared global variables called indexToUse and finalAnswer.

where the length is an odd number and when it is divided by 2, returns a decimal. That being so, I created a conditional statement and said If the length is divisible by 2 is greater or equals to 1, inside my if statement i assigned indexToUse to be length/2, then i used the Math.floor to round it down to the nearest whole number, therefore I assigned finalAnswer to string[Math.floor(indexToUse)].

Situation 2:

where the length is an even number and returns a whole number when divided by 2. That being so, I said else if the length is divisible by 2 and remainder is 0, inside my else-if statement i assigned indexToUse to be length/2, then used indexToUse-1 and indexToUse to access the 2 middile charaters, therefore I assigned finalAnswer to string[indexToUse-1] + string[indexToUse].
tags: