https://programmers.co.kr/learn/courses/30/lessons/76501
// me
// 반복문 로직을 최대한 줄였어야!
function solution(absolutes, signs) {
let answer = 0;
// 부호 반영하기
let numArr = [];
for(let i=0; i<absolutes.length; i++){
if(signs[i]=== true){
numArr.push(absolutes[i]);
}else{
numArr.push(absolutes[i]*(-1));
}
}
answer = numArr.reduce((prev, cur)=> prev + cur);
return answer;
}
// best1
function solution(absolutes, signs) {
return absolutes.reduce((acc, val, i) => acc + (val * (signs[i] ? 1 : -1)), 0);
}
// best2
function solution(absolutes, signs) {
let answer = 0;
absolutes.forEach((v, i) => {
if (signs[i]) {
answer += v;
} else {
answer -= v;
}
})
return answer;
}
// 중복 로직 제거.