Uncaught TypeError: Cannot read property 'next' of undefined
what does this error means..
Here i am just stuck with this problem please help me to get out of this error
I have commented where i am getting the error please someone help me in this error if you run this code in your browser you can also see the error
function getSadInterval(){
return Date.now()+1000;
}
function getGoneInterval(){
return Date.now() + Math.floor(Math.random()*18000) + 2000;
}
function getHungryInterval(){
return Date.now() + Math.floor(Math.random()*3000) + 2000;
}
const moles = [
{
status: "sad",
next: getSadInterval(),
king: false,
node : document.getElementById('hole-0')
},
{
status: "sad",
next: getSadInterval(),
king: false,
node: document.getElementById('hole-1')
},
{
status: "sad",
next: getSadInterval(),
king: false,
node: document.getElementById('hole-2')
},
{
status: "sad",
next: getSadInterval(),
king: false,
node: document.getElementById('hole-3')
},
{
status: "sad",
next: getSadInterval(),
king: false,
node: document.getElementById('hole-4')
},
{
status: "sad",
next: getSadInterval(),
king: false,
node: document.getElementById('hole-5')
},
{
status: "sad",
next: getSadInterval(),
king: false,
node: document.getElementById('hole-6')
},
{
status: "sad",
next: getSadInterval(),
king: false,
node: document.getElementById('hole-7')
},
{
status: "sad",
next: getSadInterval(),
king: false,
node: document.getElementById('hole-8')
},
{
status: "sad",
next: getSadInterval(),
king: false,
node: document.getElementById('hole-9')
}
];
function getNextStatus(mole){
switch (mole.status){
case "sad":
mole.next = getSadInterval();
mole.status = 'leaving';
mole.node.children[0].src = '../image/mole-leaving.png';
break;
case "leaving":
mole.next = getGoneInterval();
mole.status = 'gone';
mole.node.children[0].classList.add("gone");
break;
case "gone":
mole.status = 'hungry'
mole.next = getHungryInterval();
mole.node.children[0].classList.remove("gone");
mole.node.children[0].classList.add("hungry");
mole.node.children[0].src = '../image/mole-hungry.png';
break;
case "hungry":
mole.status = 'sad'
mole.next = getSadInterval();
mole.node.children[0].classList.remove("hungry");
mole.node.children[0].src = '../image/mole-sad.png';
break;
}
};
let runAgainAt = Date.now()+100;
function nextAction(){
const now = Date.now();
if(runAgainAt <= now){
for(let i = 0; i <= moles.length; i++){
if(moles[i].next <= now){ /**here in line i am getting error**/
getNextStatus(moles[i]);
}
}
runAgainAt = now + 100;
}
requestAnimationFrame(nextAction);
};
nextAction();