5_hyun 2022. 5. 16. 22:19
반응형
const getRange = document.querySelector(".Range input");
const getNumInput = document.querySelector(".NumInput input");
const playBtn = document.querySelector('.NumInput Button');
const Result = document.querySelector('.Result span');

playBtn.addEventListener('click', () => {
        if (getRange.value && getNumInput.value &&
            getNumInput.value >= 0) {
            const RandomValue = Math.round(Math.random() * getRange.value);
            let WinLose = "";
            if (getNumInput.value >= RandomValue) {
                WinLose = "won";
            } else {
                WinLose = "lost"
            }

            Result.innerText = `You chose: ${getNumInput.value}, the machine chose: ${RandomValue}.
            You ${WinLose}!`;
        }
    }
)

아직 document하고 셀렉터 불러오는 것을 더 연습해야겠다. 그리고 getRange.value와 getNumInput.value의 조건을 처음에 이벤트리스너 밖에 해줬는데 이러면 안되고 안에 해줘야한다는 것을 깨달았다.

반응형