[JS] 기초 - 연습 (8)

nana·2023년 1월 20일
0

👩🏻‍💻 연습

목록 보기
8/10
post-thumbnail

👩🏻‍💻 끝말잇기 게임

  • DOM 객체 다루기

✅ 순서도


✅ CodePen


✅ 순서도 최적화하기 (OR)

// 중복되는 코드
if () {
word = newWord;  // 입력한 단어가 제시어가 된다
  $word.textContent = word;
  $input.value = '';
  $input.focus();
  const order = Number($order.textContent);  // 현재 순서
  if (order + 1 > number) {
    $order.textContent = 1;
  } else {
    $order.textContent = order + 1;
  }
};

// ❗️ OR
const onClickButton = () => {
  if (!word || word[word.length - 1] === newWord[0]) {  
    // 제시어가 비어 있는가? 또는 단어가 올바른가?
    // 비어있다
    word = newWord;  // 입력한 단어가 제시어가 된다
    $word.textContent = word;
    const order = Number($order.textContent);  // 현재 순서
    if (order + 1 > number) {
      $order.textContent = 1;
    } else {
      $order.textContent = order + 1;
    }
  } else {
    alert('올바르지 않은 단어입니다!');
  }
  // 중복되는거 밖으로 빼주기
  $input.value = '';
  $input.focus();  
};

✅ 논리 연산자

📎 ||(OR), &&(AND), !(NOT)

profile
✧ 중요한건 꺾이지 않는 마음 🔥 ᕙ(•ө•)ᕤ 🔥

0개의 댓글