Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How do I validate function input as a number?

I want an alert box to appear whenever the user inputs anything other than a number n into the function. This is my code so far

<html>
<head>
<title>Intro to JavaScript</title>
</head>
<center> <h1> Task 5 </h1> </center>
<hr>
<body>
<script language="JavaScript" type="text/javascript">
function oddNumberFunc(n) {
for (i=0; i<n; i++){

    if (i % 2 !==0){
    document.write (i + " ");
} var row = (i % 20); 
    if (row ==0){
    document.write ("<br>");

} 
}
if (NaN(n)){
alert ("Please input a numerical value.");
}

}

oddNumberFunc(abc);

</script>

</body> 

</html>

The function works fine when I call it with an actual numerical value, but I receive a reference error of "abc is undefined". I think right now the code is passing 'abc' as a variable that i obviously don't have defined.

Any suggestions on how can I get this working properly?

Thanks,

James

3 Answers

Your first example should work fine, you just didn't use isNaN properly.

function oddNumberFunc(n) {
  for ( i = 0; i < n; i++ ) {
    //only print odd numbers
      if ( i % 2 !== 0 ) {
      document.write (i + " ");
      } //end if
  }//end for

  if (isNaN(n)){
    alert ("Please input a numerical value.");
  }//end if
}
oddNumberFunc("cool");

Thank you. Just to be sure, if I take this function and apply it to a form where the user will enter input, it will still work if say the user doesn't put what they type into quotes?

I think you'd want to include the if statement first. This way it checks to see if the input is a number and if it is it runs the code you want. Otherwise it pops up an alert.

if(n.isInteger == true) {
  //do this
} else {
  alert("You need to enter in a number");
}

No luck . Here's the code

<html>

<head>
<title>Intro to JavaScript</title>
</head>

<center> <h1> Task 5 </h1> </center>
<hr>

<body>

<script language="JavaScript" type="text/javascript">

function oddNumberFunc(n) {

for (i=0; i<n; i++){


if (n.isInteger == true){



if (i % 2 !==0){
document.write (i + " ");
}//end

var row = (i % 20); 

if (row ==0){
document.write ("<br>");

}// end


} // end isNan statement

else {
    alert ("Please input a numerical value.");


} // end  else statement

}//end loop


}//end function


oddNumberFunc(abc);

</script>




</body> 

</html>

Hi James, Yes, it should work as long as the input is not a number. I tried it out on codepen using different things and always got the error message unless I entered a number.

Ok thanks again man!