문제
정수의 숫자와 문자열 표현이 혼합된 배열을 취하는 JavaScript 함수를 작성해야 합니다.
우리 함수는 ok 문자열 정수를 더하고 문자열이 아닌 정수의 합계에서 빼야 합니다.
예시
다음은 코드입니다 -
const arr = [5, 2, '4', '7', '4', 2, 7, 9];
const integerDifference = (arr = []) => {
let res = 0;
for(let i = 0; i < arr.length; i++){
const el = arr[i];
if(typeof el === 'number'){
res += el;
}else if(typeof el === 'string' && +el){
res -= (+el);
};
};
return res;
};
console.log(integerDifference(arr)); 출력
다음은 콘솔 출력입니다 -
10