enclose in brackets javascript code example
Example: Enclose in Brackets
function encloseInBrackets = str => `(${str})`
function encloseInBrackets = str => {
str = str.split(''), str.splice(0,0,'(') && str.splice(str.length,0,')')
return str.join('').toString()
}
function encloseInBrackets = str => {
str = str.split('')
if(str[0] === '(') str[0]
else str.splice(0,0,'(')
if(str[str.length - 1] === ')') str[str.length - 1]
else str.splice(str.length,0,')')
return str.join('').toString()
}
function encloseInBrackets = str => {
str = str.split('')
if(str[0] === '(') str[0]
else str.unshift('(')
if(str[str.length - 1] === ')') str[str.length - 1]
else str.push(')')
return str.join('').toString()
}
console.log(encloseInBrackets('brackets)'))