showing results for - "enclose in brackets"
Lisa
03 Sep 2018
1// METHOD ONE // Statically add brackets
2function encloseInBrackets = str => `(${str})`
3
4// METHOD TWO // Statically add brackets
5function encloseInBrackets = str => {
6    str = str.split(''), str.splice(0,0,'(') && str.splice(str.length,0,')')
7    return str.join('').toString()
8}
9
10// METHOD THREE // Dynamically add the brackets
11function encloseInBrackets = str => {
12    str = str.split('')
13    if(str[0] === '(') str[0]
14    else str.splice(0,0,'(')
15    if(str[str.length - 1] === ')') str[str.length - 1]
16    else str.splice(str.length,0,')')
17    return str.join('').toString()
18}
19
20// METHOD FOUR // Dynamically add the brackets
21function encloseInBrackets = str => {
22    str = str.split('')
23    if(str[0] === '(') str[0]
24    else str.unshift('(')
25    if(str[str.length - 1] === ')') str[str.length - 1]
26    else str.push(')')
27    return str.join('').toString()
28}
29console.log(encloseInBrackets('brackets)'))