gpt4 book ai didi

javascript - 为什么我收到 Uncaught TypeError : Cannot read property 'sequence' of undefined

转载 作者:行者123 更新时间:2023-12-02 22:00:49 25 4
gpt4 key购买 nike

这里是一段代码,用于通过我的网络应用程序幻灯片跟踪和处理用户的进度,一切似乎都工作正常,直到我到达该函数的第9次执行。此时我收到此错误:

Uncaught TypeError: Cannot read property 'sequence' of undefined

不幸的是,我不知道如何调试代码或意识到导致错误的原因,我需要有人来修复它。

看起来这个 for 循环导致了错误,但为什么呢?

 // Fill planedSlides array
for(let a = 1; a < current_slide; a++){
if(slides[a - 1].sequence == nextSequence && slides[a - 1].plan == true){
console.log('a: ' + a)
planedSlides.push(a);
}
}

// Global Variables initiate at course start and modified throught the slides
// sequence A =>> serie 1 ---- serie 1 ---- serie 3 ---- serie 4

var slides = [

{ id: 1, difficulty: 2, performance: 20, guided_phrases: [], sequence: "A", plan: false, planned: 0 },
{ id: 2, difficulty: 4, performance: 30, guided_phrases: [], sequence: "A", plan: true, planned: 0 },
{ id: 3, difficulty: 4, performance: 40, guided_phrases: [], sequence: "A", plan: false, planned: 0 },
{ id: 4, difficulty: 4, performance: 20, guided_phrases: [], sequence: "B", plan: true, planned: 0 },
{ id: 5, difficulty: 4, performance: 50, guided_phrases: [], sequence: "B", plan: true, planned: 0 },
{ id: 6, difficulty: 2, performance: 20, guided_phrases: [], sequence: "A", plan: false, planned: 0 },
{ id: 7, difficulty: 4, performance: 30, guided_phrases: [], sequence: "A", plan: true, planned: 0 },
{ id: 8, difficulty: 4, performance: 40, guided_phrases: [], sequence: "B", plan: false, planned: 0 },
{ id: 9, difficulty: 4, performance: 20, guided_phrases: [], sequence: "B", plan: false, planned: 0 },

];

var current_slide; // assigned at each slide start
let lastViewedSlide;
let lastSlide = slides.length; // last slide of the course
let courseEnded = false;

// Test Assignments
current_slide = 1;


// Global Variable of progress()
let queue = [];

// At each end of slide we just initiate the progress function..
function progress(e){

// Check if we are in intro or outro
if(e){

switch(e){
// Intro slides with negative numbers incrementing.. slide 0 is the first slide..
case -1:
goToSlide(-1);
break;

case -2:
goToSlide(-2);
break;

case -3:
goToSlide(-3);
break;

// Outro slides with positive number beyond 100..
case 101:
goToSlide(101);
break;

case 102:
goToSlide(102);
break;

case 103:
goToSlide(103);
break;

// last intro slide executes progress like this: progress(1);
case 1:
// Go to first slide and generate queue
const firstSequence = slides[0].sequence;
let iterate = 0;

while(slides[iterate].sequence == firstSequence){
iterate++
queue.push(iterate);
}

goToSlide(1);

break;


};

return;
// return the whole progress function if we are in intro or outros..
}

// Check if we have reached last course slide
if(current_slide == lastSlide){
courseEnded = true; // it'll remain true
};

if(courseEnded === true){
progressCourseEnded();
return;
};

// initiate progress here
queue.shift(); // Remove this slide from queue

if(queue.length !== 0){
goToSlide(queue[0]); // if the queue is NOT empty go to the first element of queue
} else {
// If the queue is empty fill it..
fillQueue();

// Then view the queue slide by slide
goToSlide(queue[0]);

}



// fillQueue function to generate queue again
function fillQueue(){

let nextSequence = slides[current_slide].sequence;
let iterate = current_slide;
let planedSlides = [];
let newSlides = [];

// Fill planedSlides array
for(let a = 1; a < current_slide; a++){
if(slides[a - 1].sequence == nextSequence && slides[a - 1].plan == true){
console.log('a: ' + a)
planedSlides.push(a);
}
}

// Fill newSlides array
while(slides[iterate].sequence == nextSequence){
iterate++
newSlides.push(iterate)
}

// Finally fill queue
queue = planedSlides.concat(newSlides);

};




}

// goToSlide function test
function goToSlide(slide){
console.log(slide);
current_slide = slide;
//let player = GetPlayer();
//player.SetVar("goToSlide",slide);

};




// Tests
setTimeout(() => progress(1), 2000); // at slide intro

setTimeout(() => progress(), 4000); // executed at end of slide 1

setTimeout(() => progress(), 6000); // executed at end of slide 2

setTimeout(() => progress(), 8000); // executed at end of slide 3

setTimeout(() => progress(), 10000); // executed at end of slide 4

setTimeout(() => progress(), 12000); // executed at end of slide 5

setTimeout(() => progress(), 14000); // executed at end of slide 2

setTimeout(() => progress(), 16000); // executed at end of slide 6

setTimeout(() => progress(), 18000); // executed at end of slide 7 .. This throws the error

最佳答案

我认为,在下面的 while 循环中进行迭代会超出范围,因为迭代从幻灯片数组索引 1 开始:

// Fill newSlides array
while(slides[iterate].sequence == nextSequence){
iterate++;
newSlides.push(iterate);
}

我会在 while 语句之前和内部有一个 console.log 语句。

修改while循环

while(iterate < slides.length && slides[iterate].sequence == nextSequence){

关于javascript - 为什么我收到 Uncaught TypeError : Cannot read property 'sequence' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59884763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com