TIL

정예서·2일 전

My_TIL

목록 보기
12/13

오! 늘! 도! 소스해석!


먼저 이 함수는 총 가격을 계산하는 로직입니다!

document.querySelectorAll('.placed-sticker').forEach(sticker => {
  const price = parseInt(sticker.dataset.price) || 0;
  total += price;
});

.placed-sticker 클래스를 가진 모든 요소를 가져옴
각 요소의 data-price 값을 읽어서 숫자로 변환
parseInt(...) || 0 → 값이 없거나 NaN이면 0 처리
total에 계속 더함

즉, 화면에 놓인 스티커들의 가격을 모두 더하는 부분

document.getElementById('totalPrice').textContent = total;

id="totalPrice" 요소에 총합을 텍스트로 출력

const btn = document.getElementById('completeBtn');

완료 버튼 가져오기

if (total > currentPoints) {
  btn.disabled = true;
  btn.textContent = '완료 (포인트 부족)';
}

총 가격이 현재 보유 포인트(currentPoints)보다 크면

버튼 비활성화
텍스트: "완료 (포인트 부족)"

else {
  btn.disabled = false;
  btn.textContent = '완료';
}

포인트가 충분하면

버튼 활성화
텍스트: "완료"

return total; 총 가격을 반환합니다!! 끄읕

2개의 댓글

comment-user-thumbnail
2일 전

잘했다앙~

1개의 답글