Jan28 - Switch Alphabet

Tia Hwang·2020년 1월 30일
0

daily JS

목록 보기
3/5

1. 무엇을 만들었는가?

  • 영어단어 대문자는 소문자로, 소문자는 대문자로 만들기

2. HTML

  <body>
    <div class="container">
      <h1>Switch Alphabet</h1>
      <input type="text" class="word js-word" />
      <br />
      <input type="button" value="Switch!" class="switched js-btn" />
      <p class="js-result"></p>
    </div>
    <script src="index.js"></script>
  </body>

3. CSS

body {
  box-sizing: border-box;
  color: #4682b4;
}
.container {
  border: 2px dashed #708090;
  width: 300px;
  height: 300px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
}
.container .word {
  all: unset;
  height: 20px;
  padding: 10px 15px;
  font-size: 14px;
  outline: none;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.075);
  border: 1px solid rgba(70, 130, 180, 0.2);
  border-radius: 20px;
  text-align: center;
}
.container .word:focus {
  border-color: #4682b4;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.075),
    0 0 5px rgba(70, 130, 180, 0.5);
}
.container .switched {
  all: unset;
  color: #fff;
  background: #708090;
  text-transform: uppercase;
  cursor: pointer;
  padding: 5px 10px;
  font-size: 12px;
  border-radius: 10px;
  border: 1px solid #708090;
}
.container .switched:hover,
.container .switched:active {
  background: #fff;
  color: #708090;
  border: 1px solid #708090;
}

4. Javascript

const word = document.querySelector(".js-word");
const button = document.querySelector(".js-btn");
const result = document.querySelector(".js-result");

function handleClick() {
  const wordTyped = word.value;
  const capitalLetter = wordTyped.match(/[A-Z]/g);
  const smallLetter = wordTyped.match(/[a-z]/g);

  if (capitalLetter === null) {
    result.innerText = wordTyped.toUpperCase();
  } else if (smallLetter === null) {
    result.innerText = wordTyped.toLowerCase();
  } else {
    const c = capitalLetter.length;
    const s = smallLetter.length;
    if (c >= s) {
      result.innerText = wordTyped.toUpperCase();
    } else {
      result.innerText = wordTyped.toLowerCase();
    }
  }
}

button.addEventListener("click", handleClick);

5. 완성된 화면 ⬇️⬇️

jan28-js.png

6. 느낀점

  • 만약 단어가 모두 대문자이거나 소문자로 이루어져 있을 시 출력이 안되는 오류 수정
  • 좀 더 깔끔하게 만들고 싶었는데 ㅠㅠ 좀 더 공부하고 다시 다듬어 봐야 겠다

GitHub☺️

profile
프론트엔드 개발자로 취업하기

0개의 댓글