A random number between 1 and 4 that's not another random number

后端 未结 6 1343
失恋的感觉
失恋的感觉 2021-01-17 05:47

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         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-17 06:06

    How about a while loop instead? Initialise a2 = a1 and then:

    while(a2 == a1) {
        a2 = Math.floor(Math.random() * 4 + 1);
    }
    

提交回复
热议问题