https://programmers.co.kr/learn/courses/30/lessons/76501
// 1
function solution(absolutes, signs) {
let answer = 0;
let numbers = [];
for(let i = 0; i < signs.length; i++) {
if(signs[i] === true) {
numbers.push(absolutes[i]);
} else {
numbers.push(-absolutes[i]);
}
}
for(let i = 0; i < numbers.length; i++) {
answer += numbers[i];
}
return answer;
}
// 2
function solution(absolutes, signs) {
let answer = 0;
for(let i = 0; i < signs.length; i++) {
if(signs[i] === true) {
answer += absolutes[i];
} else {
answer -= absolutes[i];
}
}
return answer;
}
// 3
function solution(absolutes, signs) {
let answer = 0;
signs.map((sign, i) => {
sign === true ? answer += absolutes[i] : answer -= absolutes[i];
})
return answer;
}
// 다른 분의 풀이
function solution(absolutes, signs) {
return absolutes.reduce((acc, val, i) => acc + (val * (signs[i] ? 1 : -1)), 0);
}