๐Ÿ’ป์šฉ๋‡ฝ ๊ฐœ๋ฐœ ๋…ธํŠธ๐Ÿ’ป
article thumbnail
๋ฐ˜์‘ํ˜•

Clean Code ๋ฏธ์…˜ Day 11

DAY 11

๐Ÿ”– ๋ฏธ์…˜: ๋”๋Ÿฌ์šด ์ฝ”๋“œ๋ฅผ ๊ณ ์น˜์ž


๐Ÿ’ฉ ๋”๋Ÿฌ์šด ์ฝ”๋“œ

const merry = document.querySelector(".js-clock");

function getClock() {
	const christmas = new Date("2021, 12, 25");
	const date = new Date();
	const timeGap = christmas - date;

	const xDay = Math.floor(timeGap / (1000 * 60 * 60 * 24));
	const xHours = Math.floor(
	(timeGap - xDay * 1000 * 60 * 60 * 24) / (1000 * 60 * 60)
	);
	const xMinutes = Math.floor((timeGap % (60 * 60 * 1000)) / (60 * 1000));
	const xSeconds = Math.floor((timeGap % (60 * 1000)) / 1000);

	const day = String(xDay).padStart(2, "0");
	const hours = String(xHours).padStart(2, "0");
	const minutes = String(xMinutes).padStart(2, "0");
	const seconds = String(xSeconds).padStart(2, "0");

	merry.innerText = `${day}d ${hours}h ${minutes}m ${seconds}s`;
}

getClock();
setInterval(getClock, 1000);

๐Ÿค” ํด๋ฆฐ ์ฝ”๋“œ

setInterval(printXmasTime, 1000);

function printXmasTime() {
  const clock = document.querySelector('.js-clock');
  const { day, hours, minutes, seconds } = getXmasTime();
  clock.innerText = `${day}d ${hours}h ${minutes}m ${seconds}s`;
}

function getXmasTime() {
  const xMasDate = new Date('2021, 12, 25');
  const date = new Date();
  const timeGap = xMasDate - date;

  const SET_DAY = 1000 * 60 * 60 * 24;
  const SET_HOURS = 1000 * 60 * 60;
  const SET_MINUTES = 1000 * 60;
  const SET_SECONDS = 1000;

  const xMasTime = {
    day: setPadStart(Math.floor(timeGap / SET_DAY)),
    hours: setPadStart(Math.floor((timeGap - xDay * SET_DAY) / SET_HOURS)),
    minutes: setPadStart(Math.floor((timeGap % SET_HOURS) / SET_MINUTES)),
    seconds: setPadStart(Math.floor((timeGap % SET_MINUTES) / SET_SECONDS)),
  };

  return xMasTime;
}

function setPadStart(number) {
  return String(number).padStart(2, '0');
}

๐Ÿ‘€ ๋ฏธ์…˜ ํ›„๊ธฐ

์ผ๋‹จ ์ฝ”๋“œ์˜ ๋กœ์ง์„ ์ตœ๋Œ€ํ•œ ๊ฑด๋“ค์ง€ ์•Š์œผ๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ๊ฐœ์„  ํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค. 

์™œ๋ƒํ•˜๋ฉด ๋ชจ๋‘ ๊ณ ์ณ๋ฒ„๋ฆฌ๋ฉด ๋ฆฌํŒฉํ† ๋ง์ด ์•„๋‹ˆ๋ผ ๊ทธ๋ƒฅ ๋‚ด๊ฐ€ ์ƒˆ๋กœ ์งœ๋Š” ๋Š๋‚Œ์ด๋ผ..?

'ํด๋ฆฐ ์ฝ”๋“œ'์ฑ…์—์„œ ๋ฐฐ์šด ๋‚ด์šฉ๊ณผ ๊ฐ™์ด ๋‚ด๊ฐ€ ์ ์šฉํ•œ ๋‚ด์šฉ์€

  • ํ•จ์ˆ˜๋Š” ์ตœ๋Œ€ํ•œ ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ๋งŒ ํ•˜๋„๋ก ๋ถ„๋ฆฌํ•จ
  • ์ค‘๋ณต๋˜๋Š” ๋‚ด์šฉ์„ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋กœ ์ž‘์„ฑํ•จ
  • ์‹ ๋ฌธ์„ ์ฝ๋“ฏ์ด ์œ„์—์„œ ์•„๋ž˜๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ฝ์„ ์ˆ˜ ์žˆ๋„๋ก ํ•จ์ˆ˜์˜ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•จ
  • ๋ณ€์ˆ˜, ํ•จ์ˆ˜ ์ด๋ฆ„ ๋‚ด์šฉ ์ข€ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ๋ณ€๊ฒฝํ•จ
  • ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ ์œ„์น˜์™€ ํ•ด๋‹น ๋ณ€์ˆ˜์˜ ์œ„์น˜๋ฅผ ๊ฐ€๊น๊ฒŒ ์œ„์น˜์‹œํ‚ด
๋ฐ˜์‘ํ˜•
profile

๐Ÿ’ป์šฉ๋‡ฝ ๊ฐœ๋ฐœ ๋…ธํŠธ๐Ÿ’ป

@์šฉ๋‡ฝ

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!