firstHalf

Easy · String Manipulations · 3 test cases

Given a string, return the first half of the string. If the string length is odd, return the first half rounded down.

Examples

firstHalf('Hello') → 'He'
firstHalf('abcdef') → 'abc'
firstHalf('ab') → 'a'

Starter code

function firstHalf(str) {

}

Complexity of the optimal solution

Time O(n), space O(n). The `substring` method creates a new string. In the worst case, this new string can be up to half the length of the original string (n). Creating this new string takes O(n) time and O(n) space.

Solve firstHalf in the browser