简体   繁体   中英

Javascript: How can I check DOM input value as a parameter in if statement

I'm recently watching JavaScript online course, and I built the 'pig-game'. There's input box for set the winning score. I want to improve it if user types the value that is not 'number', then it's value will change automatically to '100' as default. I put if statement there, but I can't solve It's parameter. eg if(input === number) doesn't work.

You can check my github ( https://github.com/wonkooklee/pig-game ) and code is below

//
document.querySelector('.btn-hold').addEventListener('click', function() {


if (gamePlaying) {
    scores[activePlayer] += roundScore;
    document.getElementById(`score-${activePlayer}`).textContent = scores[activePlayer];
let input = document.getElementById('scoreSet').value;
let winningScore;

if (input === number) {  // This is that I'm dealing with
  winningScore = input;
} else {
  document.getElementById('scoreSet').value = '100';
}

if (scores[activePlayer] >= winningScore) {

  document.getElementById(`name-${activePlayer}`).textContent = 'WINNER!';
  document.querySelector(`.player-${activePlayer}-panel`).classList.add('winner');
  document.querySelector(`.player-${activePlayer}-panel`).classList.remove('active');
  diceDOM.style.display = 'none';
  gamePlaying = false;
} else {
  nextPlayer();
}

  }

});

Here is what you want (if what you want is to check if an input has been enter by the user the value will not be "" (which would be falsy), so the test if(input) will be true):

 document.querySelector('.btn-hold').addEventListener('click', function () { if (gamePlaying) { scores[activePlayer] += roundScore; document.getElementById(`score-${activePlayer}`).textContent = scores[activePlayer]; let input = document.getElementById('scoreSet').value; let winningScore; if (input) { winningScore = input; } else { document.getElementById('scoreSet').value = '100'; } if (scores[activePlayer] >= winningScore) { document.getElementById(`name-${activePlayer}`).textContent = 'WINNER;'. document.querySelector(`.player-${activePlayer}-panel`).classList;add('winner'). document.querySelector(`.player-${activePlayer}-panel`).classList;remove('active'). diceDOM.style;display = 'none'; gamePlaying = false; } else { nextPlayer(); } } });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM