[miniProjects] 16_Drink Water

๋ณด๋ฆฌยท2023๋…„ 6์›” 16์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
17/47

16_Drink Water

๐Ÿ’ป ์ฃผ์ œ : ๋ฌผ๋ณ‘์„ ํด๋ฆญํ•˜๋ฉด ํฐ ๋ฌผ๋ณ‘ ์•ˆ์— ๋ฌผ์ด ์ฑ„์›Œ์ง„๋‹ค.

  • Small ์ปต์„ ํด๋ฆญ์‹œ Big ์ปต์— ๋ฌผ์ด ๋‹ด๊ธด๋‹ค.



// smallCups์˜ ๊ฐœ์ˆ˜๋Š” ์ด 8๊ฐœ. ์ธ๋ฑ์Šค๋Š” 0~7. ํด๋ฆญ์‹œ ํ•˜์ด๋ผ์ดํŠธ ํšจ๊ณผ๋ฅผ ์ค„๊ฑฐ์ž„.
smallCups.forEach((cup, idx)=> {
  cup.addEventListener('click', () => highlightCups(idx));
})

// ์ธ๋ฑ์Šค๊ฐ€ ์ž‘์€ ์ปต๋“ค์„ ํ†ต๊ณผํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ํŠน์ • ๋ฐ˜๋ณต๋ฌธ์ด ํด๋ฆญํ•œ ์ธ๋ฑ์Šค๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€์ง€ ํ™•์ธ.
function highlightCups(idx) {
  // smallCups์˜ ๋ฐฐ์—ด์—์„œ ํ˜„์žฌ ์ธ๋ฑ์Šค(ํด๋ฆญํ•œ ์ธ๋ฑ์Šค)๋ฅผ ํ™•์ธํ•˜๊ณ  ๋ฐฐ์—ด ๋ชฉ๋ก์—์„œ ํด๋ฆญํ•œ ์ธ๋ฑ์Šค๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.(contains ๋ฉ”์„œ๋“œ)
  // ํŠน์ • ํด๋ž˜์Šค๋ฅผ ํ™•์ธํ•  ๋•Œ full์„ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ๋‹ค์Œ ์ธ๋ฑ์Šค๊ฐ€ full์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š”์ง€๋„ ํ™•์ธํ•œ๋‹ค.
  if(smallCups[idx].classList.contains('full') && !smallCups[idx].nextElementSibling.classList.contains('full')) {
    idx--;

  }

  smallCups.forEach((cup, idx2) => {
    if(idx2 <= idx) {
      cup.classList.add('full');
    } else {
      cup.classList.remove('full');
    }
  })
  updateBigCup();
}
function updateBigCup() {
  // ์ฑ„์›Œ์ง„ ์ž‘์€ ์ปต์˜ ๊ฐœ์ˆ˜๋ฅผ fullCups๋ผ๋Š” ๋ณ€์ˆ˜์— ์„ ์–ธ.
  const fullCups = document.querySelectorAll('.cup-small.full').length;

  const totalCups = smallCups.length;

  if(fullCups === 0) {
    percentage.style.visibility = 'hidden';
    percentage.style.height = 0;
  } else {
    percentage.style.visibility = 'visible';
    percentage.style.height = `${fullCups / totalCups * 330}px`;
    percentage.innerText = `${fullCups / totalCups *100}%`;
  }

  // ์ปต์ด ๋‹ค ์ฐจ๋ฉด remained ๊ธ€์ž๊ฐ€ ์—†์–ด์ ธ์•ผ ํ•จ.
  if(fullCups === totalCups) {
    remained.style.visibility = 'hidden';
    remained.style.height = 0;
  } else {
    remained.style.visibility = 'visible';
    // ์•„์ง ์ฑ„์›Œ์•ผ ํ•˜๋Š” ๋ฆฌํ„ฐ์–‘์„ ๋ณด์—ฌ์คŒ.
    liters.innerText = `${2 - (250 * fullCups / 1000)}`;
  }

}
profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

0๊ฐœ์˜ ๋Œ“๊ธ€