brackets code example

Example 1: is brackets a good code editor

yes. I actually do prefer it to VScode, Sublime Text, Komdo Edit, and others. This is a great choice.

Example 2: Enclose in Brackets

// METHOD ONE // Statically add brackets
function encloseInBrackets = str => `(${str})`

// METHOD TWO // Statically add brackets
function encloseInBrackets = str => {
    str = str.split(''), str.splice(0,0,'(') && str.splice(str.length,0,')')
    return str.join('').toString()
}

// METHOD THREE // Dynamically add the brackets
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()
}

// METHOD FOUR // Dynamically add the brackets
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)'))