Extract first half of string

// Write a function that takes a string (a) as argument
// Extract the first half a
// Return the result
function
myFunction
(
a
)
{

return
}
Test Cases:
myFunction('abcdefgh')
Expected
'abcd'
myFunction('1234')
Expected
'12'
myFunction('gedcba')
Expected
'ged'

How to solve it

This challenge can be solved with the slice method that we introduced here.
Remember that the slice method is used to extract a part of a string. We can define which part of the string we want to extract by defining a starting point and an end point. The following example extracts the first three characters of a given string:
const str = 'abcdefgh';
console.log(str.slice(0,3));
// output: 'abc'
In this scenario, the extraction includes the beginning of the string because we specified 0 as our starting point. The result includes all characters from the beginning of the string until the third character.
If we want to get the first half of the entire string, we give the end point a number that equals half the number of characters in the entire string. In the example from above, we know that the string abcdefgh has 8 characters. Hence, we can extract the first half the following way:
const str = 'abcdefgh';
console.log(str.slice(0,4));
// output: 'abcd'
But, the problem that we face in the present challenge is that the number of characters is not the same in each test case. So, we have to dynamically identify the correct number of characters.
This can be done using the length property of a JavaScript string. The length of a string is the same as the number of characters (this includes whitespace characters). So, in order to get the number of characters in the string abcdefgh, we can simply execute:
const str = 'abcdefgh';
console.log(str.length);
// output: 8
This is all you need to solve this challenge. We only need to define the end point of our slice method as half the length of the string.