Why is Settimeout not being called?

Can anyone tell me why settimeout is not being called? console.log(‘test’) gets called but not console.log(‘Game lost’)

if(posY === y && posX === x){
if(node.content === ‘o’ && node.content === ‘o’){
setTimeout(() => {
console.log(‘Game lost’);
}, 1000);
console.log(‘test’);
}

            node.content = '*';
        }

What is that supposed to mean?

BTW, the New User Guide should have information on how to post code samples in the forums. Please read it.

2 Likes

This is my entire script. it is for the find my hat project. but the point is that (node.content === ‘o’ && node .content === ‘o’) is true and yet the setTimeout is not being called.

const prompt = require(‘prompt-sync’)({sigint: true});

const goalY = 4;
const goalX = 4;
let posY = 5;
let posX = 0;

class Node {
xpos;
ypos;
content;
}

let grid = ;
const gridSize = 10;
const gridContent = [
‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,
‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,
‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,
‘’,‘’,‘’,‘’,‘’,‘’,‘o’,‘’,‘’,‘’,
‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,
‘’,‘’,‘’,‘o’,‘’,‘’,‘’,‘’,‘’,‘’,
‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,
‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,
‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘>’,‘’,
‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’
]

function updateGrid(){

for(let y = 0; y < gridSize; y++){
    for(let x = 0; x < gridSize; x++){
        if(x === 0 && y === 0){
            grid = [];
        }
        
        let node = new Node;
        node.xpos = x;
        node.ypos = y;
        grid.push(node);

        node.content = gridContent[x*y];

        

        if(node.content === ''){
            node.content = '+';
        }
        
        
        if(posY === y && posX === x){
            if(node.content === 'o' && node.content === 'o'){
                setTimeout(() => {
                    console.log('Game lost');
                }, 1000);
                console.log('test');
            }

            node.content = '*';
        }

        


        if(x === 0){
            process.stdout.write('\n');
        }

        process.stdout.write(node.content);

        
    }
}

}

function getInput(){
let input = null;
input = prompt('Input: ');

if(input === 's'){
    posY ++;
    updateGrid();
    getInput();
}

if(input === 'w'){
    posY --;
    updateGrid();
    getInput();
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
if(input === 'd'){
    posX ++;
    updateGrid();
    getInput();
}

if(input === 'a'){
    posX --;
    updateGrid();
    getInput();
}

}

updateGrid();
getInput();

Please read the New User Guide. Your code means nothing to anyone if it is not formatted.

2 Likes