For some reason the following code doesn\'t work.
var a1 = Math.floor(Math.random()*4+1);
//Answer2
for(a2 = 0; a2 != a1 && a2 != 0; a2 = Math.floo
The logic of your test is inverted. For loops continue to execute while the test is true, not if it fails. Rather than: a2 != a1 && a2 != 0
you should have (a2 == a1) || (a2 == 0)
. Though also keep in mind that the alert will also only be executed in that case when a2 is an invalid state, though an alert following the for should be valid.
Or if you're looking for the fun math-y way to do it using modular arithmetic (no retries necessary):
a2 = (Math.floor(Math.random() * 3 + (a1 + 1)) % 4) || 4