What steps should I take to fix the Uncaught TypeError that states I cannot read the 'length' property of my undefined Global variable?

After pasting the code below into my Chrome browser console, I expected to see a table dynamically generated and filled with content from an array. However, despite what I consider to be valid code, I keep receiving the following error message:

Uncaught TypeError: Cannot read property 'length' of undefined at :15:31

Here is the code I am using:

var array = [["A1", "B1", "C1"],
        ["A2", "B2", "C2"],
        ["A3", "B3", "C3"],
        ["A4", "B4", "C4"],
        ["A5", "B5", "C5"],
        ["A6", "B6", "C6"],
        ["A7", "B7", "C7"]],

table = document.getElementById("table");

for(var i = 0; i < array.length; i++);
{
   //=== Adds a new Row
   var newRow = table.insertRow(table.length);

   for(var j = 0; j < array[i].length; j++);
   {
       //=== Adds a new cell
       var cell = newRow.InsertCell(j);

       //=== Adds value to the cell
       cell.innerHTML = array[i][j];
   }
}

Upon clicking on the 15:31 in the error message, it appears that the issue lies within the array[i].length section of the second loop.

From my experiences with these kinds of error messages, it seems like the variable may not be globally defined. In this case, my code clearly shows that the array variable is defined globally. Can anyone shed light on why it's still not recognized as such and provide a solution?

Answer №1

Looks like a few errors have crept in.

  1. You mistakenly added semicolons after the two for loops.

    for(var i = 0; i < values.length; i++)

    for(var j = 0; j < values[i].length; j++)

  2. There is a typo where insertCell function should be used instead of InsertCell.

    var cell = newRow.insertCell(j);

To fix it, remove the unnecessary semicolons and correct the function name as mentioned above.

Answer №2

Keep an eye out for your semicolons. Check it out:

var array = [1, 2, 3, 4];

for(var i = 0; i < array.length; i++)
{
  console.log(i);
}

Adding a semicolon at the end of the for statement creates a for loop without a body and with block scope:

var array = [1, 2, 3, 4];

for(var i = 0; i < array.length; i++); // It will only increment i until i >= array.length
{
  console.log(i); // This will output 4
  // Accessing array[i] will return undefined, leading to errors
  // Trying to get length of undefined will throw an error
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting to employ the HTML5 file picker feature within the AppMaker platform

Hey there, I'm currently working on integrating the HTML5 file picker into my AppMaker app. (Since my app needs to run as the developer, I can't use Drive Picker). I've been able to display the file picker in an HTML widget using the follow ...

Set only 3 fields to be editable and the rest to be read-only

I need to modify my code for marking fields on a PDF as read-only so that 3 specific fields remain editable if they are empty. How can I adjust the code to exclude these 3 fields if they contain no input? var currentPageNum = event.target.page; for (va ...

Issue encountered when attempting to develop a countdown timer using Typescript

I am currently working on a countdown timer using Typescript that includes setting an alarm. I have managed to receive input from the time attribute, converted it using .getTime(), subtracted the current .getTime(), and displayed the result in the consol ...

Customizing ExtJS 4.1: Mastering field overrides

Seeking guidance on applying a plugin to all fields(numberfield, textfield, datefield, etc.) within the ExtJS 4.1 library. Does anyone have suggestions on how to achieve this? I understand that all fields are derived from BaseField. I attempted the follow ...

Skybox images failing to display

Learning THREE.js has been quite challenging for me, especially when trying to display a simple skybox. Despite attempting various strategies, I have not been successful due to insufficient documentation and multiple releases. This struggle has left me fee ...

remove a row from a table within a Firestore database

I'm currently working on implementing a button to delete a specific row in my table from the Firebase database, but I'm uncertain about how to achieve this using a button function. My code is written in JavaScript and utilizes Firestore. Below i ...

The Angular Button fails to display when all input conditions are met

I'm currently working on validating a form using this link. The requirement is simple - when an input field is invalid, the `save` button should be disabled. Conversely, when all input fields are valid, the `SAVE` button should be enabled/shown. The & ...

Function that yields promise result

I need help figuring out how to make this recursive function return a promise value. I've attempted various approaches, but they all resulted in the search variable ending up as undefined. public search(message: Message) { let searchResult: strin ...

Having trouble locating HTML elements by their id when typing into a box using cy.get in Cypress with JavaScript

I am a novice in working with cypress and HTML, and I am currently attempting to enter an address in the specified address field. <input type="text" title="Street Address 1" name="billing[street][]" id="billing:street1" value="" class="input-text " ...

JQuery Touchscreen Resizability

Currently, I am utilizing the jQuery resizable method to adjust the size of a div element. While this functionality works seamlessly with a mouse, it does not seem to work on touchscreen devices. The resizable cursor is not displayed and resizing is not po ...

Redirecting files from the file system to the emulator is not functioning properly

I am new to Phonegap and need help resolving this issue. In my application, I need to redirect to the List.html file when a button is clicked. Here is the code I have written: button1 function test() { window.location="/home/swift-03/phonegapexa ...

Images cannot be uploaded using ajax

I have been troubleshooting an issue with uploading image files using ajax without refreshing the page. However, I am facing a problem where the file does not get moved to the specified folder even after implementing basic PHP code for file upload. if($_S ...

The right way to create elements with jQuery syntax

$(<div>, { 'class': 'inner', 'html': $('<p>').text('Id: ' + toManage[i].id) }).appendTo($element); Currently, I am dynamically creating the above code. However, I am looking to modify it to ...

The React functional component fails to update when triggered by a parent component's setState method

My React component is utilizing apollo to fetch data through graphql class PopUpForm extends React.Component { constructor () { super() this.state = { shoptitle: "UpdateMe", popupbodyDesc: "UpdateMe" } } re ...

Angular directive ng-template serves as a component type

In the code snippet below, <!DOCTYPE html> <html> <head> ..... </head> <body ng-app="app"> <script type="text/ng-template" id="my-info-msg.html"> <s ...

Discovering in VB.Net whether a number exceeds three times in an array

Currently working with Visual Basic, I've encountered an issue with my code. I have an array containing 9 numbers, and I want to ensure that no number appears more than 3 times... Dim PArray= New Integer() {30, 50, 100, 200, 500, 1000, 2000, 5000} Di ...

Including a JavaScript file using script tags can cause issues with jQuery UI

I've been working on this issue for quite some time now and it's proving to be quite challenging. The problem I'm facing involves incorporating jQuery UI's datepicker into my HTML page which is already being controlled by some Javascri ...

Having trouble with the Ajax load function not functioning in your HTML code?

The issue has been resolved. Thank you to everyone who helped! Initially, I was attempting to run a file offline instead of on a web server (XAMPP server). Once I uploaded the file to the web server, it started working properly. I had been trying to load ...

Obtain the content of the text in the row before by pressing the button within the

After researching answers to similar questions, I have been unable to find a solution that works for my specific situation. I am facing an issue with retrieving the text value of a dynamically appended row with the class idRow. The challenge arises when t ...

How to grab the information from a different webpage using jQuery

This code snippet shows the function in question: $.get(profileAddress, function(data){ alert(data); }) The purpose of this function is to display the source code of the page profileAddress in an alert message. Now, how can I ...