This is the 'conversation' I had with the browser as I tried to find the bug - my input is highlighted in lime. Try it yourself and hopefully realise that my error is that g_n=3 is very different to g_n==3.

> g_n
1
> g_n++
1
> g_n
2
> if (g_n=3) g_n=1;
1
> g_n
1
> g_n=3
3
> g_n
3
> g_n==3
true
> if (g_n==3) g_n=1;
1
> g_n++
1
> g_n
2
> if (g_n==3) g_n=1;
undefined
> g_n
2

This is a mistake I make a lot because many languages don't require the double equals.