๊ฐœ๋ฐœ ๐Ÿพ/Playground

ํ‚คํŒจ๋“œ๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅํ•˜๊ธฐ (HTML+SASS+JavaScript)

JOTOKKI 2021. 7. 8. 15:17
728x90

ํ‚ค๋ณด๋“œ์—์„œ ์ž…๋ ฅ์„ ๋ฐ›์ง€ ์•Š๊ณ  ์ง์ ‘ ํ‚คํŒจ๋“œ๋ฅผ ํด๋ฆญํ•˜์—ฌ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ฒดํฌํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 

์ฐธ๊ณ ๋กœ ๋‹จ์ˆœํžˆ ์›น๋‹จ์—์„œ ์ •ํ•ด์ง„ ๊ฐ’์„ ์ž…๋ ฅ๊ฐ’๊ณผ ๋น„๊ตํ•˜๋Š” ๋กœ์ง๋งŒ ์žˆ์Šต๋‹ˆ๋‹ค. 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” OOP๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. 

๊ฒฐ๊ณผํ™”๋ฉด

 

 

1. ๋งˆํฌ์—… ์Šคํƒ€์ผ

<div class="pwWrap">
    <div class="pwSection">
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
        <p class="message">&nbsp;</p>
    </div>
    <div class="numberSection">
        <button class="number">1</button>
        <button class="number">2</button>
        <button class="number">3</button>
        <button class="number">4</button>
        <button class="number">5</button>
        <button class="number">6</button>
        <button class="number">7</button>
        <button class="number">8</button>
        <button class="number">9</button>
        <button class="number">0</button>
    </div>
</div>

1-1. SCSS์‚ฌ์šฉ์‹œ

$errorColor: red;
$confirmColor: green; 

.pwWrap {
  width: 80%; 
  max-width: 450px;
  background: lightGrey; 
  margin: 20px auto;
  
  .pwSection {
    position: relative; 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    width: 100%; 
    height: 150px; 
    .dot {
      display: block; 
      width: 10px; 
      height: 10px; 
      background: darkgrey;
      border-radius: 100%; 
      margin: 0 5px;
      &.active {
        background: rgba(0,0,0,0.7);
      }
    }
    .message {
      position: absolute; 
      bottom: 5px; 
      left: 0; 
      z-index: 1; 
      min-width: 100%;
      text-align: center; 
      font-size: 14px;
      font-weight: bold;
      letter-spacing: -0.03em;
      opacity: 0; 
      transition: .2s ease-out;
    }
  }
  .numberSection {
    overflow: hidden;
    .number {
      float: left; 
      width: 33.33%;
      border: 1px solid rgba(0,0,0,0.1); 
      padding: 12px 0;
      cursor: pointer; 
      background: #F8F2F2;
      &:last-child {
        margin-left: 33.33%;
      }
    }
  }
  
  &.error {
     .message {
        opacity: 1; 
        color: $errorColor; 
      }
  }
  &.confirm {
     .message {
        opacity: 1; 
        color: $confirmColor; 
      }
  }
}

1-2. CSS ์‚ฌ์šฉ ์‹œ

.pwWrap {
  width: 80%;
  max-width: 450px;
  background: lightGrey;
  margin: 20px auto;
}

.pwWrap .pwSection {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 150px;
}

.pwWrap .pwSection .dot {
  display: block;
  width: 10px;
  height: 10px;
  background: darkgrey;
  border-radius: 100%;
  margin: 0 5px;
}

.pwWrap .pwSection .dot.active {
  background: rgba(0, 0, 0, 0.7);
}

.pwWrap .pwSection .message {
  position: absolute;
  bottom: 5px;
  left: 0;
  z-index: 1;
  min-width: 100%;
  text-align: center;
  font-size: 14px;
  font-weight: bold;
  letter-spacing: -0.03em;
  opacity: 0;
  transition: .2s ease-out;
}

.pwWrap .numberSection {
  overflow: hidden;
}

.pwWrap .numberSection .number {
  float: left;
  width: 33.33%;
  border: 1px solid rgba(0, 0, 0, 0.1);
  padding: 12px 0;
  cursor: pointer;
  background: #F8F2F2;
}

.pwWrap .numberSection .number:last-child {
  margin-left: 33.33%;
}

.pwWrap.error .message {
  opacity: 1;
  color: red;
}

.pwWrap.confirm .message {
  opacity: 1;
  color: green;
}

 

2. ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑํ•˜๊ธฐ

์ €๋Š” ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 

 

1. ์ˆซ์ž ๋ฒ„ํŠผ ํด๋ฆญ, ํด๋ฆญ๋œ ์ˆซ์ž๋ฅผ ๋ฐฐ์—ด์— ๋‹ด๊ธฐ

2. ๋ฒ„ํŠผ ํด๋ฆญ ํšŸ์ˆ˜ ์ฒดํฌ

3. ๋ฒ„ํŠผ ํด๋ฆญ ํšŸ์ˆ˜๊ฐ€ 4์ผ ๋•Œ ๋„˜์–ด์˜จ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ์ž…๋ ฅํ•œ ๊ฐ’ ๋น„๊ต

4. 3๋ฒˆ ๋น„๊ต๊ฐ€ ํ‹€๋ฆฌ๋ฉด ๋ฆฌ์…‹ ๋ฐ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ

5. 3๋ฒˆ ๋น„๊ต๊ฐ€ ๋งž์œผ๋ฉด ์„ฑ๊ณต ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ

 

pwCheck๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ ๋’ค, ๋‚ด๋ถ€์— ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. 

ํ•„์š”ํ•œ ๊ฐ์ฒด๋“ค๋„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. 

function PwCheck(pw) { 
    const _this = this; 
    _this.pwStr = pw.toString(); // ๋ฌธ์ž, ์ˆซ์ž์—ด์„ ๋ชจ๋‘ ํ—ˆ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ฌด์กฐ๊ฑด ํ•œ๊ฐ€์ง€ ํƒ€์ž…์œผ๋กœ ๋งž์ถค
    _this.password = []; // ์ง€์ •๋œ ํŒจ์Šค์›Œ๋“œ
    _this.passwordNumber = []; // ์ž…๋ ฅํ•  ํŒจ์Šค์›Œ๋“œ
    _this.cnt = 0; // ์ž…๋ ฅํšŸ์ˆ˜ ์ฒดํฌ
    _this.compChk = false; // ์ž…๋ ฅ์™„๋ฃŒ ์ฒดํฌ 
    _this.msg = [
        'Wrong Password! Try Again! ๐Ÿ‘ฟ',
        'Success! ๐Ÿ˜'
    ]; 
    _this.parent = document.querySelector('.pwWrap');
    _this.dots = document.querySelectorAll('.dot');
    _this.numbers = document.querySelectorAll('.number');
    _this.message = document.querySelector('.message');
    
}

์—ฌ๊ธฐ์„œ this๋Š” pwCheckํ•จ์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ, this๋ฅผ _this์•ˆ์— ๋‹ด์•˜๊ธฐ ๋•Œ๋ฌธ์—, 

์•ˆ์—์„œ ์„ ์–ธํ•œ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋“ค์€ ์™ธ๋ถ€์—์„œ ๋ฐ”๋กœ ์ ‘๊ทผ์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 

 

PwCheck์—์„œ ๋„˜๊ฒจ๋ฐ›๋Š” pw๋ฅผ ์‹ค์ œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. 

pw๋ฅผ ๊ตณ์ด string์œผ๋กœ ๋ฐ›๋Š” ์ด์œ ๋Š” ๋„˜์–ด์˜ค๋Š” pw๊ฐ€ ์ˆซ์ž์ธ ๊ฒฝ์šฐ ๋ฐฐ์—ด์— ๋„ฃ์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 

๋ฐ˜๋“œ์‹œ ๋ฐฐ ์—ด๋Œ€ ๋ฐฐ์—ด๋กœ ๋น„๊ตํ•  ํ•„์š”๋Š” ์—†์ง€๋งŒ ์ €๋Š” ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๋ฐฐ์—ด์— ๋‹ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด๋ผ๋ฆฌ ๋น„๊ต๋ฅผ ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. 

 

๋จผ์ € ๋ฐ›์€ pw๊ฐ’์„ ๋ฐ›์ž๋งˆ์ž ๋ฐฐ์—ดํ™” ํ•ฉ๋‹ˆ๋‹ค.

    // ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ฐฐ์—ด์— ๋„ฃ์Œ 
    _this.getPw = function(){
        for(let i=0; i<_this.pwStr.length; i++) {
            _this.password[i] = _this.pwStr[i];
        }
    }

์ด๋ ‡๊ฒŒ ์„ ์–ธํ•œ ๋’ค ์ฆ‰์‹œ ์‹คํ–‰์‹œ์ผœ์ฃผ๊ธฐ ์œ„ํ•ด initํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค ๊ฒ๋‹ˆ๋‹ค. 

init๋‚ด๋ถ€ ํ•จ์ˆ˜๋“ค์€ ๋ชจ๋‘ ์ฆ‰์‹œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

    _this.init = function(){
        _this.getPw();
    }();

 

๊ทธ๋ฆฌ๊ณ  ์ฆ‰์‹œ ์‹คํ–‰๋˜์–ด์•ผ ํ•  ํ•จ์ˆ˜๊ฐ€ ํ•˜๋‚˜  ๋” ์žˆ์Šต๋‹ˆ๋‹ค. 

๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ์ž…๋‹ˆ๋‹ค. 

{...}    
    
    // ์ˆซ์ž๋ฒ„ํŠผ click์ด๋ฒคํŠธ ์—ฐ๋™
    _this.handleListener = function(){
        if(!_this.compChk) {
            _this.numbers.forEach(function(number){
                number.addEventListener('click', function(){console.log("number")});
            })
        }
    }
    
    _this.init = function(){
        _this.handleListener();
        _this.getPw();
    }();

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ๊ฐ ํด๋ฆญํ•œ ๋ฒ„ํŠผ์„ ์ œ๋Œ€๋กœ ๊ฐ€์ง€๊ณ  ์˜ต๋‹ˆ๋‹ค. 

์ด๋•Œ ์›๋ž˜๋Š” id๋ฅผ ๋‘๊ฑฐ๋‚˜ ๋”ฐ๋กœ attribute๋ฅผ ๋”ฐ๋กœ ์„ค์ •ํ•˜์—ฌ value๊ฐ’์„ ์ง€์ •ํ•˜์ง€๋งŒ, ์ €๋Š” ๊ทธ๋ƒฅ textContent๋กœ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.

 

handleNumber๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋‘๊ณ  ์—ฌ๊ธฐ์— ํด๋ฆญํ•œ ์ˆซ์ž๋ฅผ ๋„˜๊ธธ ๊ฒ๋‹ˆ๋‹ค. 

ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค passwordNumber ๋ฐฐ์—ด์— ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด์ฃผ๊ณ  ์นด์šดํŠธ๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ค๋‹ˆ๋‹ค. 

๋งŒ์•ฝ ์นด์šดํŠธ๊ฐ€ 4๊ฐ€ ๋œ๋‹ค๋ฉด, handleResultํ•จ์ˆ˜๋กœ ๋„˜๊ฒจ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  UI๋ฅผ ๋‹ด๋‹นํ•˜๋Š” handleDotActive๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋‘๊ณ  ์ž…๋ ฅํ•œ ์ˆซ์ž๋Š” ๋™๊ทธ๋ผ๋ฏธ ์ƒ‰์„ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. 

 

    // ์ˆซ์žํ‚ค๋ฅผ ๋ˆŒ๋ €์„๋•Œ ์ด๋ฒคํŠธ 
    _this.handleNumber = function(number){
        _this.passwordNumber[_this.cnt] = number.textContent;
        _this.handleDotActive(true);
        _this.cnt++; //  _this.passwordNumber[_this.cnt] ๋„ฃ์–ด์ค€ ํ›„์— ์ฆ๊ฐ€์‹œ์ผœ์ฃผ์„ธ์š”!
        if(_this.cnt === 4) {
            _this.handleResult();
        }
    }
    // dot ํ™œ์„ฑํ™” 
    _this.handleDotActive = function(type){
        if(type) {
            _this.dots.forEach(function(dot, i){
                if(i === _this.cnt) dot.classList.add('active'); 
            })
        } else {
            _this.dots.forEach(function(dot){
               dot.classList.remove('active'); 
            })
        }
    }
    // ๊ฒฐ๊ณผ์ฒ˜๋ฆฌ 
    _this.handleResult = function(){
        // ๊ฒฐ๊ณผ์ฒ˜๋ฆฌํ•˜๋Š” ๊ตฌ๊ฐ„!
    }

handleDotActive์—์„œ type์„ ๋ฐ›๋Š” ์ด์œ ๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ ธ์„ ๊ฒฝ์šฐ ๋ฆฌ์…‹์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์„ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค. ์ด์ œ handleResult๊นŒ์ง€ ๋งˆ๋ฌด๋ฆฌํ•ด๋ด…์‹œ๋‹ค. 

์ €๋Š” ๋น„๊ต๋งŒ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋‘์–ด boolean๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 

    // ๋น„๋ฐ€๋ฒˆํ˜ธ ๋น„๊ต
    _this.handleCheckPw = function(){
        let compare = JSON.stringify(_this.password) === JSON.stringify(_this.passwordNumber);
        return compare; 
    }
    // ๊ฒฐ๊ณผ์ฒ˜๋ฆฌ 
    _this.handleResult = function(){
        if(_this.handleCheckPw()) { 
        	// ๋น„๋ฐ€๋ฒˆํ˜ธ ๋งž์œผ๋ฉด
            _this.parent.classList.add('confirm');
            _this.message.textContent = _this.msg[1];
            _this.compChk = true;
        } else {
        	// ๋น„๋ฐ€๋ฒˆํ˜ธ ํ‹€๋ฆฌ๋ฉด
            _this.parent.classList.add('error');
            _this.message.textContent = _this.msg[0];
            // ์ž…๋ ฅ์ƒํƒœ ์ดˆ๊ธฐํ™” 
            _this.passwordNumber = [];
            _this.cnt = 0; 
            setTimeout(function(){
                _this.parent.classList.remove('error');
                _this.handleDotActive(); // ๋™๊ทธ๋ผ๋ฏธ UI ์ดˆ๊ธฐํ™”
            }, 800);
        }
    }

JSON.stringify๋กœ ์ „์ฒด ๋ฌธ์ž๋ฅผ ๋น„๊ตํ•ด์•ผ ์ˆœ์„œ๊นŒ์ง€ ํ†ต์งธ๋กœ ๋น„๊ต๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 

( for๋ฌธ์œผ๋กœ๋„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๊ฒŒ ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ณ  ํŽธํ•ด์š”! )

๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ ธ์„ ๋•Œ setTimeout์„ ํ†ตํ•ด ์—๋Ÿฌ ๋ฌธ๊ตฌ๋ฅผ ์ž ์‹œ ๋ณด์—ฌ์ค€ ๋’ค ๋‹ค์‹œ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ์ƒํƒœ ์ดˆ๊ธฐํ™”๋ฅผ ์‹œ์ผœ์ค๋‹ˆ๋‹ค. 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์™„์„ฑ์ธ ๊ฒƒ ๊ฐ™์ง€๋งŒ! ๐Ÿค”

 

์—๋Ÿฌ ๋ฌธ๊ตฌ๊ฐ€ ๋– ์žˆ๋Š” ๋™์•ˆ ๋‹ค๋ฅธ ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ๋ฒ„๋ฆฐ๋‹ค๋ฉด, ๊ทธ ๋ˆŒ๋ฆฐ ๊ฐ’์ด ์Œ“์—ฌ ๋ฒ„๋ฆฌ๊ฒŒ ๋ผ์š”!

๊ทธ๋ž˜์„œ ์—๋Ÿฌ ๋ฌธ๊ตฌ๊ฐ€ ๋– ์žˆ๋Š” ๋™์•ˆ์—๋Š” ํ‚ค ์ž…๋ ฅ์„ ํ•ด๋„ ๋™์ž‘์ด ๋˜์ง€ ์•Š๋„๋ก ๋ง‰์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

๊ทธ๋ž˜์„œ _this.comp๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋†“์•˜์Šต๋‹ˆ๋‹ค. 

 

{...}

    // ์ˆซ์ž๋ฒ„ํŠผ click์ด๋ฒคํŠธ ์—ฐ๋™
    _this.handleListener = function(){
        if(!_this.compChk) { // ์ถ”๊ฐ€!
            _this.numbers.forEach(function(number){
                {...}
            })
        }
    }
    
    // ์ˆซ์žํ‚ค๋ฅผ ๋ˆŒ๋ €์„๋•Œ ์ด๋ฒคํŠธ 
    _this.handleNumber = function(number){
        if(!_this.compChk) { // ์ถ”๊ฐ€
            _this.passwordNumber[_this.cnt] = number.textContent;
            {...}
        }
    }
    
        // ๊ฒฐ๊ณผ์ฒ˜๋ฆฌ 
    _this.handleResult = function(){
        if(_this.handleCheckPw()) {
           {...}
            _this.compChk = true;
        } else {
            {...}
            // ์ผ์‹œ์ ์ธ ํด๋ฆญ ๋ฐฉ์ง€ 
            _this.compChk = true;
            setTimeout(function(){
                _this.compChk = false; // ๋‹ค์‹œ false๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํด๋ฆญ์ด๋ฒคํŠธ๊ฐ€ ์ž‘๋™๋˜๊ฒ ์ง€์š”?
                {...}
            }, 800);
        }
    }

{...}

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋น„๋กœ์†Œ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค! 

ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค. 

let pwCheck = new PwCheck(1234);

 


ํ”Œ๋Ÿฌ์Šค๋กœ ์‹ฌ์‹ฌํ•ด์„œ(?) ํ‚ค ์ž…๋ ฅ๊ฐ’๋„ ๋ฐ›์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. 

์‚ฌ์‹ค ํ‚ค๋ณด๋“œ ์ž…๋ ฅ์œผ๋กœ ํ•  ๊ฑฐ๋ผ๋ฉด ๊ตณ์ด ํ‚คํŒจ๋“œ๊ฐ€ ํ•„์š” ์—†๋Š”๋ฐ, ๊ทธ๋ƒฅ ์ด์œ  ์—†์ด ๋งŒ๋“ค์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๐Ÿคฃ

๊ธฐ๋ณธ ๋กœ์ง์€ ๋ชจ๋‘ ๊ฐ™๊ณ  ๋ฒ„ํŠผ ํด๋ฆญ์„ ํ‚ค๋ณด๋“œ ํด๋ฆญ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

๋˜ํ•œ ํ‚ค ์ž…๋ ฅ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ํ‚คํŒจ๋“œ UI๋„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค. 

 

handleListenerํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๊ณ ,  handleButtonActive๋ฅผ ๋งŒ๋“ค์–ด ํ‚ค๋ณด๋“œ ํด๋ฆญ ์‹œ ํ‚คํŒจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋˜๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

    // ํ‚ค๋ณด๋“œ click์ด๋ฒคํŠธ ์—ฐ๋™ (๋ณ€๊ฒฝ)
    _this.handleListener = function(){
        window.addEventListener('keydown', function(e){
            _this.handleButtonActive(e.key);      
            _this.handleNumber(e.key);  
        })
    }

    // ๋ฒ„ํŠผํ™œ์„ฑํ™” (์ถ”๊ฐ€)
    _this.handleButtonActive = function(key){
        _this.numbers.forEach(function(number){
            if(number.textContent === key) number.style.background = "lightblue";
            setTimeout(function(){ number.style.background = "#F8F2F2" }, 140);
        })
    }

๊ทธ๋ฆฌ๊ณ  handleListener์—์„œ e.key๋ฅผ ๋„˜๊ฒผ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— handleNumber์—์„œ๋Š” number.textContent๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์™”์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ๋ƒฅ number๋กœ ์ˆ˜์ •ํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

    // ์ˆซ์žํ‚ค๋ฅผ ๋ˆŒ๋ €์„๋•Œ ์ด๋ฒคํŠธ 
    _this.handleNumber = function(number){
        if(!_this.compChk) { // 999
            // _this.passwordNumber[_this.cnt] = number.textContent;
            _this.passwordNumber[_this.cnt] = number;  // <- ์ด๋ ‡๊ฒŒ ์ˆ˜์ •
            {...}
        }
    }

๊ทธ๋Ÿผ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 

 

 

๋ฐ˜์‘ํ˜•