Javascript multi statement -


i building little app js , find difficult that:

i want users input integer number range 0-100. if it's not int parsing int. want put number function checknum, , there appears problem. let me paste code:

$(function () {  initapp();  function getinput(){     var userinput = window.prompt("input number range 0-100");     var num = parseint(userinput);     return num; };   function checkinput(num){     if( num < 0 || num > 100){         displayerror();     }     else{         alert('num ok');     } };  function displayerror(){   alert("error"); };  function initapp(){     getinput();     checkinput(); }; 

});

as can see if put number out of range there alert says num ok - not true. have no idea doing wrong. i'll appreciate help. more, i'd put here function number.isnan() provide typing non-sense in input. thought statement this:

if(number.isnan(num) || num <0 || num > 100){...} 

i guess mistake obvious , trivial more experienced me.

to answer question, you're passing nothing checkinput() it's undefined, , undefined < 0 || undefined > 100 false. solution pass returned value getinput() checkinput() so:

function initapp(){     var value = getinput();     checkinput(value); } 

to answer you're aiming for: use <input type="number"> , have browser work you.

<form>    <input type="number" min="0" max="100" step="1" />    <button>try submit!</button>  </form>

no javascript required.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -