끝말잇기 게임 | 다음 사람에게 순서 넘기기

uoah·2023년 2월 1일

Training

목록 보기
5/20
post-thumbnail

다음 사람에게 순서를 넘긴다.

현재 순서 파악
현재 순서에 1을 더한 값이 number보다 큰가?
-> 예 : 다음 순서를 1로
-> 아니오 : 다음 순서를 현재 순서 +1 로

const number = Number(prompt('참가 인원을 입력해 주세요.'));
const $button = document.querySelector('button');
const $input = document.querySelector('input');
const $word = document.querySelector('#word');
const $order = document.querySelector('#order');
let word; // 제시어
let newWord; //새로 입력한 단어

const onClickButton = () => {
  if (!word){
    // 비어 있는 경우
    word = newWord;
    $word.textContent = word;
    $input.value = '';
    const order = Number($order.textContent); // 현재 순서
    if (order + 1 > number){
      $order.textContent = 1;
    } else {
      $order.textContent = order + 1;
    }
  } else {
    // 비어 있지 않은 경우
    if(word[word.length - 1] === newWord[0]) {
      // 단어가 올바른가
      word = newWord;
      $word.textContent = word;
      $input.value = '';
      const order = Number($order.textContent); // 현재 순서
      if (order + 1 > number){
        $order.textContent = 1;
      } else {
        $order.textContent = order + 1;
      }
    }else {
      // 올바르지 않은가
    }
  }
};

const onInput = (event) => {
  newWord = event.target.value;
};


$input.addEventListener('input', onInput);
$button.addEventListener('click', onClickButton);

2개의 댓글

comment-user-thumbnail
2023년 2월 1일

혹시 다이어그램 직접 작성하신거면 어떤 툴을 쓰셨나요?

1개의 답글