I copied exact javascript code from a class I am taking online but the code is not working properly for me. I continue to get the "cannot read property 'length' of undefined" error when try to run it. Can anyone tell me why I continue to get this error and how I can fix it.
//the JavaScript logic on this page simply adds the "visible" CSS class to the next image in the rotation appoximately every 3.5 seconds
var slideInterval = 3500;
//retrieves all of the "figure" elements within the "section" element using the "id" of 'carousel'. Returns the resulting array as the result of this function
function getFigures() {
return
document.getElementById('carousel').getElementsByTagName('figure');
}
//This function iterates over the figure elements in the section element. It removes the visible class from the current figure element, then adds the class to the next figure element. Once complete, it uses the setTimeout function to invoke itself again after a specified amount of time (3500 milliseconds = 3.5 seconds)
function moveForward() {
var pointer;
var figures = getFigures();
for (var i = 0; i < figures.length; i++) {
if (figures[i].className == 'visible') {
figures[i].className = '';
pointer = i;
}
}
if (++pointer == figures.length) {
pointer = 0;
}
figures[pointer].className = 'visible';
setTimeout(moveForward, slideInterval);
}
//In the startPlayback function, use the setTimeout function in JavaScript to invoke the moveForward method after a specified amount of time. Use the slideInterval variable for the time interval
function startPlayback() {
setTimeout(moveForward, slideInterval);
}
//invokes "startPlayback" from above
startPlayback();