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

  }

});
7
  • 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. Commented Jul 22, 2020 at 14:56
  • Check this answer: stackoverflow.com/questions/4059147/… Commented Jul 22, 2020 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. Commented Jul 22, 2020 at 15:03
  • So, you want to check if input is a number? Commented Jul 22, 2020 at 15:05
  • 2
    Does this answer your question? Check if input is number or letter javascript Commented Jul 22, 2020 at 15:06

1 Answer 1

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

    }

});

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe a little explanation would help someone.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.