473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why am I getting undefined error in my array?

2
Expand|Select|Wrap|Line Numbers
  1. <script>
  2.         window.onload=myday;
  3.  
  4.             function myday(){
  5.  
  6.             var day=new Array("sunday","monday","tuesday","wednesday","thursday","friday","saturday");
  7.  
  8.             var d=new Date();
  9.             theDay=d.getDay();
  10.  
  11.             for (i=0;i<=day.length;i++)
  12.             {
  13.                     if (i!=theDay)
  14.                 {
  15.  
  16.                 document.write("today is not " + day[i]);
  17.                 document.write("<br />");}
  18.  
  19.                 else
  20.                 {
  21.                     document.write("today is " + day[i]);
  22.                     document.write("<br />");}
  23.             }
  24.             }
  25.  
  26.  
  27. </script>
Nov 8 '10 #1

✓ answered by JKing

It's a very small error. In your for loop you have for (i=0;i<=day.length;i++)

but if you compare this to your example for(i=0;i<allmonths.length;i++) you will notice the operator <= should be <.

You want your loop to terminate before 'i' is equal to the length. This is because arrays start at 0. If you have an array with length of 5 you would have indexes 0,1,2,3,4 but not 5.

3 2297
Dormilich
8,658 Expert Mod 8TB
because you use document.write().

if you call document.write() after the page finished loading (i.e. when onload is triggered) document.write() erases* the current document (completely) and writes the document anew. at that point all your JavaScript has been erased and thus your function is not defined.


* - it does that by implicitly calling document.open()
Nov 8 '10 #2
Jen O
2
How do you recommend I get around this? I have this example from my book(below), and it uses the onload with document.write. I believe this is why I am confused.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     window.onload=initAll;
  3.  
  4.     function initAll(){
  5.     var allmonths = new Array ("january","february","march","april","may","june","july","august","september","october","november","december")
  6.     var birthmonth = prompt("what is your birth month?","");
  7.     for(i=0;i<allmonths.length;i++){
  8.         if (allmonths[i]==birthmonth){
  9.             document.write("<strong>"+(i+1)+". "+allmonths[i]+" is your birth month!"+"</strong></br >");
  10.             alert("Hooray "+allmonths[i]+" is your birth month!");
  11.         } else{
  12.             document.write((i+1)+". "+allmonths[i]+" is "+"<strong>"+"NOT"+"</strong>"+" your birth month!"+"</br >");
  13.         }
  14.     }
  15.  
  16. }
  17.     </script>
Nov 8 '10 #3
JKing
1,206 Expert 1GB
It's a very small error. In your for loop you have for (i=0;i<=day.length;i++)

but if you compare this to your example for(i=0;i<allmonths.length;i++) you will notice the operator <= should be <.

You want your loop to terminate before 'i' is equal to the length. This is because arrays start at 0. If you have an array with length of 5 you would have indexes 0,1,2,3,4 but not 5.
Nov 9 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Philip Ronan | last post by:
OK, here's my 2p worth: === Q. Why am I getting the error message 'Headers already sent'? A. PHP produces this error message when you try to set a header for a web page after you have already...
2
by: KathyB | last post by:
Hi, I'm trying to get the following script to work, but I'm getting an error saying "rowID is undefined". function showhide(rowId) { var showRow = "Edit_" + rowID var hideRow = "View_" + rowID...
8
by: VB Programmer | last post by:
I'm acutally using VB6, not VB.NET, but I couldn't find the newsgroup for version 6.... I need help for something that should be simple. I keep getting a "subscript out of range" error and...
3
by: rich | last post by:
I keep getting the error: Warning: in_array(): Wrong datatype for second argument on line 679 here is the code. I declare and fill the array $specprimA = array(); $specprimA=...
1
by: pbd22 | last post by:
Hi - I have code that automatically fills tabs on a page. In some cases, a URL path won't be correct (the folder and files don't exist) and, in these cases, I want to catch these exceptions and...
1
deephill
by: deephill | last post by:
hi... i am new to server side scripts n server technologies. I bought 1 php wordpress theme blog in online. I am installed WAMP Server. I checked in localhost. I am getting below error. I didnt...
1
by: myshona | last post by:
Hi Everybody, I am getting the error : Fatal error: Call to undefined function xslt_create().I am using php 5 and Apache 2.2 server.Please let me know how to solve this....
6
by: SunshineInTheRain | last post by:
can anyone please help me on problem of 'Sys' is undefined error? I know a lot forum has discussed about this, but none i success to solve the error. I have run it on Window server 2000, IIS 5,...
45
by: angelicdevil | last post by:
i have 2 tables 1 is status_type with field name status and other is users with field username and status now i want that the first listbox lists all status from status type ( this i have achieved...
16
by: manutd7 | last post by:
Hi, Every time I try to access the contents of the following array it returns that it is undefined. I have tried multiple variations on accessing the arrays syntax wise but get different errors...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.