Now, for generate snake on board I make one global numeric array variables ‘snake’. At starting I set snake array as [73,72,71,70] that means 73th td, 72th td, 71th td and 7th td are part of the snake and I set the background color of all these four td to black by adding snake css class (function – generateSnake()).
For generate cherry that snake need to eat will be a single td. Function generateCherry() – will generate random number withing the limit of our game console and which is not a part of snake.
Function runSnake() – for move snake in appropriate direction as per user input from keyboard arrow keys. We also have speed dropdown to set speed of snake. By using setTimeout(), runSnake method is recursively executes at certain interval as per the snake speed.