티스토리 뷰

강의/제로초

[제로초] 끝말잇기

millfeel1298 2021. 4. 28. 03:49


제로초 코드

<div>
  <span id="order">1</span>번째 참가자
</div>
<div>
  제시어:
  <span id="word"></span>
</div>
<input class="text-input" type="text">
<input class="btn" type="button" value="입력">
<script>
  const number = Number(prompt('몇 명이 참가하나요?'));
  const $order = document.querySelector('#order');
  const $word = document.querySelector('#word');
  const $textInput = document.querySelector('.text-input');
  const $btn = document.querySelector('.btn');
  let word; // 제시어
  let newWord; // 새로운 입력한 단어

  const onClickButton = () => {
    if (!word || word[word.length - 1] === newWord[0]) { // 제시어가 비어있는가?
      // 비어 있다.
      word = newWord; // 입력한 단어가 제시어가 된다.
      $word.textContent = word;
      $textInput.value = '';
      const order = Number($order.textContent); // 현재 순서
      if (order + 1 > number) {
        $order.textContent = 1;
      } else {
        $order.textContent = order + 1;
      }
    } else { // 끝글자와 일치하지 않는가
      alert('올바르지 않습니다');
    }
    $input.value = '';
    $input.focus();
  }
  const onInput = (event) => {
    newWord = event.target.value;
  };

  $textInput.addEventListener('input', onInput);
  $btn.addEventListener('click', onClickButton);
</script>

순서도 만들어보기


코딩 해보기

See the Pen [프로그램] 끝말잇기 by mill (@millfeel) on CodePen.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함