# TIL 8/5

송은우·2022년 8월 5일
0

## TIL

목록 보기
14/61
const priority = [
["+", "-", "*"],
["+", "*", "-"],
["-", "+", "*"],
["-", "*", "+"],
["*", "-", "+"],
["*", "+", "-"],
];
function spliting(expression: string) {
const temp = [-1];
let resultArray = [];
for (let i = 0; i < expression.length; i++) {
if (
expression[i] === "+" ||
expression[i] === "*" ||
expression[i] === "-"
) {
temp.push(i);
}
}
temp.push(expression.length);
for (let i = 0; i < temp.length - 1; i++) {
resultArray.push(expression.slice(temp[i] + 1, temp[i + 1]));
resultArray.push(expression[temp[i + 1]]);
}
resultArray.pop();
// for(const data of )
return resultArray;
}
function check(testing: Array<string>, prior: Array<string>) {
let changing = [];
for (let i = 0; i < testing.length; i++) {
if (i % 2) {
changing[i] = testing[i];
} else {
changing[i] = parseInt(testing[i]);
}
}

for (let operator = 0; operator < 3; operator++) {
for (let i = 1; i < changing.length; i += 2) {
if (changing[i] === prior[operator]) {
let temp = 0;
switch (prior[operator]) {
case "+":
temp = (changing[i - 1] as number) + (changing[i + 1] as number);
break;
case "-":
temp = (changing[i - 1] as number) - (changing[i + 1] as number);
break;
case "*":
temp = (changing[i - 1] as number) * (changing[i + 1] as number);
break;
}
changing.splice(i - 1, 3, temp);
i -= 2;
}
}
}
return Math.abs(changing[0] as number);
}
function solution(expression: string) {
let start = spliting(expression);
for (const prior of priority) {
let temp = check(start, prior);
}
}