Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
isNanN and if...else statements within a function using an array
Old 09-13-2008, 08:36 AM isNanN and if...else statements within a function using an array
Novice Talker

Posts: 8
Name: Mary
Trades: 0
Beginner JS person here and I have an instructor who doesn't teach this very well and a book that doesn't explain things. He has given us an assignment to use the isNaN statement within a function and the if...else statement. He wrote all the code for us and we had to replace everything with just the array name and a variable name and write the if...else statements and the alert statements. Okay so far....

My problem...I can't get past the isNaN statement and get to the rest of the if...else. So what am I doing wrong? Do I have to create another variable for the isNaN or can I use the one I already have?

var DaysOfWeek = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

function MyDayArray(DaysOfWeek){
if(isNaN(DaysOfWeek)){
alert ("Please enter a number.");
}
else{
if(DaysOfWeek > 8){
alert ("Please enter a number less than 8.");
}
else{
if(DaysOfWeeks < 0){
alert ("Please enter a number greater than 0.");
}
else{
alert ("Saturday");
}
}
}
}
Mary Anne is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-13-2008, 09:10 AM Re: isNanN and if...else statements within a function using an array
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
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...
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-13-2008, 10:03 AM Re: isNanN and if...else statements within a function using an array
Novice Talker

Posts: 8
Name: Mary
Trades: 0
Here is the original code and assignment that he gave us....

<html>
<head><title>Quiz 3</title>
<script = "javascript">
var arrayname = //replace arrayname with an appropriate array name and finish this line to create and populate the array with the days of the week
function functionName(variableName){ //replace functionName with a valid function Name, replace variableName with a valid variable name
if(isNaN(variableName)){ //replace variableName with a valid variable name
//replace with an alert method that displays the message shown in step 2a.
}
else{
if(conditional){ //replace conditional with a valid conditional as described in step 2b.i.
//replace with an alert method that displays the message shown in step 2b.i.
}
else{
if(conditional){ //replace conditional with a valid conditional as described in step 2b.ii.
//replace with an alert method that displays the message shown in step 2b.ii.
}
else{
//replace with an alert method that displays the message shown in step 2b.iii.
}
}
}
}
</script>
</head>
<body>
<form name="dayofweek">
<input type=text name="dofwk" size="1" value=1>
<input type=button name="chkday" value="Which day of the week?" //insert an event handler that calls the function above and passes the value stored in the text field "dofwk">
</form>
</body>
</html>

Then in the written instruction we have tell us what to put in the alert boxes. He doesn't explain any further why we are to do what we are to do. Funny thing is....some of this stuff hasn't even been cover yet! So we are left to guess at how to do it or search around the internet. I have had to teach myself as I go along. All his classes have been this way (Flash, ActionScript)...Thank goodnes this is my last class with him.
Mary Anne is offline
Reply With Quote
View Public Profile
 
Old 09-13-2008, 10:17 AM Re: isNanN and if...else statements within a function using an array
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
I have had to teach myself as I go along.
Welcome to the world of web-development. Most of us learned the same way.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-13-2008, 10:24 AM Re: isNanN and if...else statements within a function using an array
Novice Talker

Posts: 8
Name: Mary
Trades: 0
I don't mind teaching myself but this when it is the guise of a class that you paid for and are receiving credit for....you do expect something a little better than what I am receiving right now.

Personal, if I had to do this code, I probably would use a case statement and take out the isNaN. And I am tempted to change his code to that just to spite him!
Mary Anne is offline
Reply With Quote
View Public Profile
 
Old 09-13-2008, 03:21 PM Re: isNanN and if...else statements within a function using an array
nyef's Avatar
Ultra Talker

Posts: 265
Name: Lucas
Trades: 0
Your biggest problem is that you're using the same name for your array (DaysOfWeek) and your function parameter (DaysOfWeek). Within the scope of your function, DaysOfWeek will always refer to the parameter, and you will not be able to access the array at all.

Change the parameter name to DayNumber.
Change the isNaN statement to: if (isNaN(DayNumber))...
Change the greater than check to: if (DayNumber>6)...
(not 8... the array starts at 0 so it's 0 through 6 for the days)
Change the final alert to: alert(DaysOfWeek[DayNumber]);
__________________
~nyef

Please login or register to view this content. Registration is FREE
nyef is offline
Reply With Quote
View Public Profile Visit nyef's homepage!
 
Old 09-16-2008, 11:33 AM Re: isNanN and if...else statements within a function using an array
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
You can try it this way:

Code:
<html>
<head><title>Quiz3</title>
<script type="text/javascript">

var days_array = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
                           "Thursday", "Friday", "Saturday");

function attachHandlers(){
var the_button = document.getElementById("my_button");
the_button.onclick=checkEntry;

}

function checkEntry(){

var the_field = document.getElementById("my_day");
var the_value = the_field.value;

  if (isNaN(the_value))
  {
   alert("Please enter a number");
   return;
  }

  else if (the_value < 0 || the_value > 6)
  {
   alert("Please enter a number between 0 and 6");
   return;
  }

 alert("The day of the week you chose was " + days_array[the_value]);
 the_field.value="";
}
</script>
</head>

<body onLoad="attachHandlers()">
<form name="dayofweek">
<input type=text name="dofwk" size="1" id="my_day">
<input type=button name="chkday" value="Which day of the week?" id="my_button">
</form>
</body>
</html>
LayneMitch is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to isNanN and if...else statements within a function using an array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.73878 seconds with 12 queries