문제출처
const operator = {
    '+': (a, b) => a + b,
    '-': (a, b) => a - b,
    '*': (a, b) => a * b,
};
function solution(expression) {
    let answer = 0;
    const permutation = [
        ['+', '-', '*'],
        ['+', '*', '-'],
        ['-', '+', '*'],
        ['-', '*', '+'],
        ['*', '-', '+'],
        ['*', '+', '-']
    ];
    const numArr = expression.split(/[^0-9]/).map(num => +num);
    const opArr = expression.match(/[\+\-\*]/g);
    
    for (const permu of permutation) {
        let copyNum = [...numArr];
        let copyOp = [...opArr];
        let opIdx = 0;
        
        while (true) {
            for (let i=0; i<copyOp.length; i++) {
                if (copyOp[i] === permu[opIdx]) {
                    copyNum[i] = operator[copyOp[i]](copyNum[i], copyNum[i + 1]);
                    copyNum.splice(i + 1, 1);
                    copyOp.splice(i, 1);
                    i--;
                }
            }
            opIdx++;
            
            if (copyNum.length === 1) break;
        }
        
        if (Math.abs(copyNum[0]) > answer) answer = Math.abs(copyNum[0]);
    }
    return answer;
}
풀이
- 연산자는 3개밖에 없고 모든 경우의 수는 6개뿐이기 때문에 완전탐색으로 푼다.
 
- 처음에 정규표현식으로 expression에서 숫자와 연산자를 분리해준다.
 
- while문의 가장 안쪽 블럭에서 
i--를 해줘야 하는게 포인트...