1. JS문법종합반 5주차
2. JS 예제 week1
- 오류해결과정(1)
- 오류해결과정(2)
1.
2.
(1) 형변환 문제
나의 초기 풀이
function add(a, b) {
if (typeof a !== 'number') {
a = Number(a);
} else if (typeof b !== 'number') {
b = Number(b);
}
return a + b;
}
실행결과
console.log(result1); // 출력 결과: 10
console.log(result2); // 출력 결과: 55
해결
매개변수 a, b를 받아올 때 underscore로 받아와 선 선언하여 해결
해결 코드
function add(a, b) {
let _a = a;
let _b = b;
if (typeof a !== 'number') {
_a = Number(a);
} else if (typeof b !== 'number') {
_b = Number(b);
}
return _a + _b;
}
(2) 객체 및 배열 메소드 응용 문제
나의 초기 풀이
function handleEdit(id, editingObj) {
let element = db.find(el => el.id === id);
[...element].replace(element, editingObj);
}
에러메세지
TypeError: element is not iterable
element는 이터러블하지 않다고한다... 다시 짜보자
find를 쓰는게 해결방법이 안보이는데 findIndex를 써서 배열인덱스 가지고 해봐야하나
function handleEdit(id, editingObj) {
const index = db.findIndex(el => el.id === id);
" "
}
이 다음부터 모르겠다 밤에 문법공부 더 하고 내일 다시 고민시작
나머지 문제는 깔끔하게 해결!