All isNaN does is check if the variable is not a number, so I'm not sure I see why it needs to be applied here. However, since it is a part of the assignment, there is no need to question it. Part of what you're supposed to know in JS is values like NaN and null and undefined.
Seeing how you're checking an entire array against whether it is a number is silly, and won't work the way you have it laid out. Even if you change every day in the DaysOfWeek array to a number, 1-7, it will still throw "false" since it will return "array", not a number. What you need to do is loop through each value of the array, then check each one with isNaN:
Code:
<script type="text/javascript">
var DaysOfWeek = new Array("Sunday", "1", "9", "Wednesday", "Thursday", "Friday", "Saturday");
function MyDayArray(DaysOfWeek){
for(var i=0; i<DaysOfWeek.length; i++) {
if(isNaN(DaysOfWeek[i])){
alert ("Please enter a number.");
}
//rest of code goes here
}
}
</script>
Also, the way you have the else {} statements all lined up like that is nonsensical, although it didn't throw an error in my browser, probably only because it never got past the logic of the if(isNaN);
The whole code should look something like this:
Code:
<script type="text/javascript">
var DaysOfWeek = new Array("Sunday", "1", "9", "Wednesday", "Thursday", "Friday", "Saturday");
function MyDayArray(DaysOfWeek){
for(var i=0; i<DaysOfWeek.length; i++) {
if(isNaN(DaysOfWeek[i])){
alert ("Please enter a number.");
}
else if(DaysOfWeek[i] > 8){
alert ("Please enter a number less than 8.");
}
else if(DaysOfWeek[i] < 0){
alert ("Please enter a number greater than 0.");
}
else {
alert ("Saturday"); //what is this?
}
}
}
window.onload = function() {MyDayArray(DaysOfWeek);}
</script>
Now, try changing the values in the array to various numbers to see what happens. The code is still silly, since for some reason it was written to default to alert("Saturday"), but that is another story...