Minimum swaps for Parentheses balancing – JS

Parentheses strings are strings containing only the characters ‘(‘ and ‘)’.
A parentheses string is considered balanced when its opening parentheses align with its closing parentheses.
For example, “()()” and “(()())” are balanced parentheses strings
while “)(“, “())(” are not.
Given a string consisting of the same number of opening and closing parentheses, determine the minimum number of character swaps required to make the string a balanced parentheses string.
const minSwaps = (str) => {
const pharenteses = str.split(''); // Converting into an array
let total = pharenteses.length - 1; // To go from end to start
let pair = ''; // to concatenate first position and last position
let swaps = 0; // to count the number of swaps
// Go through every position of the array
for (let i = 0; i < pharenteses.length; i++) {
// Condition (start position < last position)
if (i < total) {
// Concatenating first and last position of the array
pair = pharenteses[i] + pharenteses[total]; // string
// If the pair is different to ()
if (pair !== '()') {
// Increment the swaps
swaps++
}
// Decrement total
total--
}
}
// Returning the swaps
return swaps
}
console.log(minSwaps(')()('))
Result:

Criss.