1

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();
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Ankit Raj
  • 11
  • 4
  • https://stackoverflow.com/questions/49723311/why-am-i-getting-typeerror-arrayi-is-undefined – Bergi Jun 15 '21 at 18:06

1 Answers1

1

Change

for(let i = 0; i <= moles.length; i++){

to

for(let i = 0; i < moles.length; i++){

The length of moles is 10, you need to iterate over moles from 0 - 9. Currently you're iterating from 0 - 10, so it errors on the last iteration, as moles[10] doesn't exist.

duncan
  • 31,401
  • 13
  • 78
  • 99