Number 배열을 받아서 두 배열에 공통적이지 않은 배열에서 요소를 반환하는 JavaScript 함수를 작성해야 합니다.
예를 들어, 두 배열이 -
인 경우const arr1 = [2, 4, 2, 4, 6, 4, 3]; const arr2 = [4, 2, 5, 12, 4, 1, 3, 34];
출력
그러면 출력은 다음과 같아야 합니다. -
const output = [ 6, 5, 12, 1, 34 ]
예시
이에 대한 코드는 -
const arr1 = [2, 4, 2, 4, 6, 4, 3];
const arr2 = [4, 2, 5, 12, 4, 1, 3, 34];
const deviations = (first, second) => {
const res = [];
for(let i = 0; i < first.length; i++){
if(second.indexOf(first[i]) === -1){
res.push(first[i]);
}
};
for(let j = 0; j < second.length; j++){
if(first.indexOf(second[j]) === -1){
res.push(second[j]);
};
};
return res;
};
console.log(deviations(arr1, arr2)); 출력
콘솔의 출력 -
[6, 5, 12, 1, 34 ]