+ 1

Game freeze/crash with no error.

After collecting 11th food, game crashes/freezes don't know what happens, but you are forced to restart whole app, cause non other codes runs after. I was trying to find place where it may happen for arround 4h with no luck. Game: https://code.sololearn.com/WJpw9z0V3lBQ/?ref=app

22nd Oct 2019, 5:05 PM
Dower
Dower - avatar
3 ответов
+ 3
Interesting question, I can experience what you mean by "you are forced to restart whole app". The problem is caused by an infinite loop. Try to add a counting variable in before your while loops and add an exit condition based on this counting variable for every while loop you implement. Try also to add a let in each for loop. For example, for(i2=0;... and for(i=0;... these will cause the i and i2 be a global scope variable and other function may change its value at the same time, making it impossible to quit the for loop. Instead, use for(let i2 = 0; i2 <.... and for(let i = 0; i <...
22nd Oct 2019, 8:45 PM
Gordon
Gordon - avatar
+ 2
OMG, thank you very much :))) Works like a charm. The main problem was, that I left one for loop, which caused infinite loop. I found it only cause of i variables that you mentioned :) = = = = = P.s. I've put let before all variables in for loops. Thanks again, it was a huge help :)
23rd Oct 2019, 4:53 PM
Dower
Dower - avatar
+ 2
You are welcome, Dower. Let me explain a little bit about what happens when we don't add var or let keyword, and also among var and let which one is preferred. When you use a variable without declaring it, it becomes a property of window object, which is also known as global variable. (See Fact #063) var is a pre-ES6 keyword for declaring functional scope variable, and will be hoisted; let is a ES6 keyword for declaring block-scope variable, and will not be hoisted. let is safer and preferred. (See Fact #023) https://code.sololearn.com/Wyr76080kKxS/?ref=app
24th Oct 2019, 12:50 AM
Gordon
Gordon - avatar