function comebackIfSnakeGoOutsideCanvas() {
    const head = snake.head()
    const OFF_THRESHOLD = snake.length() < 10 ? snake.length() / 2 : 5

    if (noOfSnakeTilesOffCanvasFromHead() >= OFF_THRESHOLD) {

        if (snakeOffCanvasDelayAfterFirstTurnCountDown > 0) {
            snakeOffCanvasDelayAfterFirstTurnCountDown--
            return
        }

        const snakeTileSize = snake.snake_tile_size
        if (head.x < -snakeTileSize) { // outside of left wall
            if (snake.isMovingUp()) snake.turnRight()
            if (snake.isMovingDown()) snake.turnLeft()
            if (snake.isMovingLeft()) {
                if (head.y <= snakeboard.height / 2) {
                    snake.turnLeft()
                } else {
                    snake.turnRight()
                }
                snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN
            }
            return
        }

        if (head.x > snakeboard.width) { // outside of right wall
            if (snake.isMovingUp()) snake.turnLeft()
            if (snake.isMovingDown()) snake.turnRight()
            if (snake.isMovingRight()) {
                if (head.y <= snakeboard.height / 2) {
                    snake.turnRight()
                } else {
                    snake.turnLeft()
                }
                snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN
            }
            return
        }

        if (head.y < -snakeTileSize) { // outside of top wall
            if (snake.isMovingLeft()) snake.turnLeft()
            if (snake.isMovingRight()) snake.turnRight()
            if (snake.isMovingUp()) {
                if (head.x <= snakeboard.width / 2) {
                    snake.turnRight()
                } else {
                    snake.turnLeft()
                }
                snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN
            }
            return
        }

        if (head.y > snakeboard.height) { // outside of bottom wall
            if (snake.isMovingLeft()) snake.turnRight()
            if (snake.isMovingRight()) snake.turnLeft()
            if (snake.isMovingDown()) {
                if (head.x <= snakeboard.width / 2) {
                    snake.turnLeft()
                } else {
                    snake.turnRight()
                }
                snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN
            }
            return
        }
    }
}
/**
 * Check if snake has gone outside of canvas and re-position the snake to come back to canvas if
 * a number of snake's tiles counted from head (*OFF_THRESHOLD*) has fully gone outside canvas
 * 
 * Snake comes back to canvas by making a U-turn: two consecutive turns with a delay in-between
 * (*SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN*) so that snake doesn't come back at a position too close
 * from where it starts moving outside canvas
 */
Check if snake has gone outside of canvas and re-position the snake to come back to canvas if
a number of snake's tiles counted from head (*OFF_THRESHOLD*) has fully gone outside canvas

Snake comes back to canvas by making a U-turn: two consecutive turns with a delay in-between
(*SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN*) so that snake doesn't come back at a position too close
from where it starts moving outside canvas
Check if snake has gone outside of canvas and re - position the snake to come back to canvas if a number of snake ' s tiles counted from head ( * OFF_THRESHOLD * ) has fully gone outside canvas Snake comes back to canvas by making a U - turn : two consecutive turns with a delay in - between ( * SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN * ) so that snake doesn ' t come back at a position too close from where it starts moving outside canvas
function comebackIfSnakeGoOutsideCanvas() {
    const head = snake.head()
    const OFF_THRESHOLD = snake.length() < 10 ? snake.length() / 2 : 5
    if (noOfSnakeTilesOffCanvasFromHead() >= OFF_THRESHOLD) {
        if (snakeOffCanvasDelayAfterFirstTurnCountDown > 0) {
            snakeOffCanvasDelayAfterFirstTurnCountDown--
            return
        }
        const snakeTileSize = snake.snake_tile_size
        if (head.x < -snakeTileSize) { 
            if (snake.isMovingUp()) snake.turnRight()
            if (snake.isMovingDown()) snake.turnLeft()
            if (snake.isMovingLeft()) {
                if (head.y <= snakeboard.height / 2) {
                    snake.turnLeft()
                } else {
                    snake.turnRight()
                }
                snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN
            }
            return
        }
        if (head.x > snakeboard.width) { 
            if (snake.isMovingUp()) snake.turnLeft()
            if (snake.isMovingDown()) snake.turnRight()
            if (snake.isMovingRight()) {
                if (head.y <= snakeboard.height / 2) {
                    snake.turnRight()
                } else {
                    snake.turnLeft()
                }
                snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN
            }
            return
        }
        if (head.y < -snakeTileSize) { 
            if (snake.isMovingLeft()) snake.turnLeft()
            if (snake.isMovingRight()) snake.turnRight()
            if (snake.isMovingUp()) {
                if (head.x <= snakeboard.width / 2) {
                    snake.turnRight()
                } else {
                    snake.turnLeft()
                }
                snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN
            }
            return
        }
        if (head.y > snakeboard.height) { 
            if (snake.isMovingLeft()) snake.turnRight()
            if (snake.isMovingRight()) snake.turnLeft()
            if (snake.isMovingDown()) {
                if (head.x <= snakeboard.width / 2) {
                    snake.turnLeft()
                } else {
                    snake.turnRight()
                }
                snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN
            }
            return
        }
    }
}
function comebackIfSnakeGoOutsideCanvas ( ) { const head = snake . head ( ) const OFF_THRESHOLD = snake . length ( ) < 10 ? snake . length ( ) / 2 : 5 if ( noOfSnakeTilesOffCanvasFromHead ( ) >= OFF_THRESHOLD ) { if ( snakeOffCanvasDelayAfterFirstTurnCountDown > 0 ) { snakeOffCanvasDelayAfterFirstTurnCountDown -- return } const snakeTileSize = snake . snake_tile_size if ( head . x < - snakeTileSize ) { if ( snake . isMovingUp ( ) ) snake . turnRight ( ) if ( snake . isMovingDown ( ) ) snake . turnLeft ( ) if ( snake . isMovingLeft ( ) ) { if ( head . y <= snakeboard . height / 2 ) { snake . turnLeft ( ) } else { snake . turnRight ( ) } snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN } return } if ( head . x > snakeboard . width ) { if ( snake . isMovingUp ( ) ) snake . turnLeft ( ) if ( snake . isMovingDown ( ) ) snake . turnRight ( ) if ( snake . isMovingRight ( ) ) { if ( head . y <= snakeboard . height / 2 ) { snake . turnRight ( ) } else { snake . turnLeft ( ) } snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN } return } if ( head . y < - snakeTileSize ) { if ( snake . isMovingLeft ( ) ) snake . turnLeft ( ) if ( snake . isMovingRight ( ) ) snake . turnRight ( ) if ( snake . isMovingUp ( ) ) { if ( head . x <= snakeboard . width / 2 ) { snake . turnRight ( ) } else { snake . turnLeft ( ) } snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN } return } if ( head . y > snakeboard . height ) { if ( snake . isMovingLeft ( ) ) snake . turnRight ( ) if ( snake . isMovingRight ( ) ) snake . turnLeft ( ) if ( snake . isMovingDown ( ) ) { if ( head . x <= snakeboard . width / 2 ) { snake . turnLeft ( ) } else { snake . turnRight ( ) } snakeOffCanvasDelayAfterFirstTurnCountDown = SNAKE_OFF_CANVAS_DELAY_AFTER_FIRST_TURN } return } } }
Check if snake has gone outside of canvas and re-position the snake to come back to canvas if
 a number of snake's tiles counted from head (*OFF_THRESHOLD*) has fully gone outside canvas
Check if snake has gone outside of canvas and re - position the snake to come back to canvas if a number of snake ' s tiles counted from head ( * OFF_THRESHOLD * ) has fully gone outside canvas
// outside of left wall // outside of right wall // outside of top wall // outside of bottom wall