lundi 25 juillet 2016

Javascript multi statement

I am building little app with JS and I find difficult with that:

I want to get from users input an integer number from range 0-100. If it's not an int I am parsing it to int. Then I want to put that number to function checkNum, and there appears a problem. Let me paste my code:

$(function () {

initApp();

function getInput(){
    var userInput = window.prompt("Input number from range 0-100");
    var num = parseInt(userInput);
    return num;
};


function checkInput(num){
    if( num < 0 || num > 100){
        displayError();
    }
    else{
        alert('num is ok');
    }
};

function displayError(){
  alert("error");
};

function initApp(){
    getInput();
    checkInput();
};

});

As you can see even if I put number out of range there is always alert says num is ok - what is not true. I have no idea what I am doing wrong. I'll appreciate any help. What is more, I'd like to put here function Number.isNaN() to provide typing non-sense in the input. I thought about statement like this:

if(Number.isNaN(num) || num <0 || num > 100){...}

I guess that my mistake is obvious and trivial for someone more experienced than me.

Aucun commentaire:

Enregistrer un commentaire