3

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. e.g. 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();
}

  }

});
  • 1
    What do you mean by `string`? Do you mean the value must be the actual text `"string"`? Because that's what you would write, with the quotes. If you want to check the *type* of the `input` variable, well, it's always going to be `"string"`, since that's what `value` returns. – Heretic Monkey Jul 22 '20 at 14:56
  • Check this answer: https://stackoverflow.com/questions/4059147/check-if-a-variable-is-a-string-in-javascript – Vova Jul 22 '20 at 14:57
  • Oops, sorry. I mean not 'string' only 'number' But DOM.value in variable recognize this as only 'string'. I think it's because in single-quote. – Wonkook Lee Jul 22 '20 at 15:03
  • So, you want to check if `input` is a number? – Heretic Monkey Jul 22 '20 at 15:05
  • 2
    Does this answer your question? [Check if input is number or letter javascript](https://stackoverflow.com/questions/18042133/check-if-input-is-number-or-letter-javascript) – Heretic Monkey Jul 22 '20 at 15:06
  • Yes, That was what I mean – Wonkook Lee Jul 22 '20 at 15:07
  • Many thanks, Heretic Monkey! (isNaN(input) === false) has solved this problem. – Wonkook Lee Jul 22 '20 at 15:13

1 Answers1

1

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();
        }

    }

});
8HoLoN
  • 1,122
  • 5
  • 14
  • 1
    Maybe a little explanation would help someone. – Amin Jafari Jul 22 '20 at 15:01
  • 2
    This answer is making an assumption about what the OP wants, but not communicating what that assumption is, which makes it a poor answer. It is almost always better to ask the user what they want, then *wait* for the OP to respond, rather than guessing. – Heretic Monkey Jul 22 '20 at 15:04