function solution(p) {
let num = [];
let x = p.split('+').map((e) => {
if (e.includes('x')) {
return e.trim().charAt(0) == 'x' ? 1 : Number(e.split('x')[0].trim());
} else {
num.push(Number(e));
return 0;
}
});
let xnum = 0;
let nnum = 0;
x.forEach(e => xnum+=e);
num.forEach(e => nnum+=e);
return nnum == 0 ? `${xnum}x` : `${xnum}x + ${nnum}`;
}
https://school.programmers.co.kr/learn/courses/30/lessons/120863
function solution(p) {
let num = [];
let x = p.split('+').map((e) => {
if (e.includes('x')) {
return e.trim().charAt(0) == 'x' ? 1 : Number(e.split('x')[0].trim());
} else {
num.push(Number(e));
return 0;
}
});
let xnum = 0;
let nnum = 0;
x.forEach(e => xnum+=e);
num.forEach(e => nnum+=e);
if(xnum==1){
return nnum == 0 ? 'x' : `x + ${nnum}`;
} else if(xnum == 0){
return nnum.toString();
} else {
return nnum == 0 ? `${xnum}x` : `${xnum}x + ${nnum}`;
}
}