0

So, this program needs to check when a user entered a decimal, it is supposed to print out "not a valid guess, try again." What is a good way to check if a user has entered a decimal in an IF statement?

My fiddle: http://jsfiddle.net/6qkdP/925/

jQuery code:

$(document).ready(function () {
    $("#chat-box").append('<div class="guess"><br>Guess a number between 1-100</div>');
});
var number = Math.round((Math.random() * 100)) % 100 + 1;

document.getElementById('number').addEventListener('keydown', function (event) {


    if (event.which === 13 || event.keyCode === 13) {
        var guess = this.value;       

            if (guess == number) {               
                $("#chat-box").append('<div class="human">' + guess + '</div>');
                $("#chat-box").append('<div class="comp">You guessed it!</div>');
            } else if (isNaN(guess)) {
                $("#chat-box").append('<div class="human">' + guess + '</div>')
                $("#chat-box").append('<div class="comp">' + guess + ' is not a valid guess. Try again.</div>');
            } else if (guess == 0) {
                $("#chat-box").append('<div class="human">' + guess + '</div>')
                $("#chat-box").append('<div class="comp">' + guess + ' is not a valid guess. Try again.</div>');
            } **else if (parseInt(guess))**  {
                $("#chat-box").append('<div class="human">' + guess + '</div>')
                $("#chat-box").append('<div class="comp">' + guess + ' is not a valid guess. Try again.</div>');
            } else if (guess < number) {
                $("#chat-box").append('<div class="human">' + guess + "</div>")
                $("#chat-box").append('<div class="comp">' + guess + " is too small, guess again.</div>");
            } else if (guess > number) {
                $("#chat-box").append('<div class="human">' + guess + "</div>")
                $("#chat-box").append('<div class="comp">' + guess + " is too big, guess again.</div>");
            } 
        }
});
HawkBlade124
  • 107
  • 1
  • 9
  • there is a `parseFloat()` method in js – Eric Guan Mar 04 '16 at 00:52
  • 1
    Possible duplicate of [Validate decimal numbers in JavaScript - IsNumeric()](http://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric) – Farside Mar 04 '16 at 01:07

3 Answers3

2

Use this.

if(guess.split('.').length>1){
    $("#chat-box").append('<div class="human">' + guess + '</div>')
    $("#chat-box").append('<div class="comp">' + guess + ' is not a valid guess. Try again.</div>');
}
Ralph John Galindo
  • 1,190
  • 6
  • 11
0

One of possible solutions, could be to use RegExp for these purposes, alike:

var isValid = /^([0-9]){1,2}(\.){1}([0-9]){2}$/.test(inputField.value); 
Farside
  • 9,923
  • 4
  • 47
  • 60
-1

In order to check if they entered a non-whole number, you need to have a data type which can receive it. if you make input a double, then you can check with :

while( input < 0 || input > 10 || (input - (int)input) > 0 ) to see if they entered a floating point value.

Seems kinda silly to do this though.