A for loop without any {}

后端 未结 6 1896
借酒劲吻你
借酒劲吻你 2020-12-10 18:10

So I was reading about shuffling an array. And then I came across this script:

shuffle = function(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(         


        
6条回答
  •  无人及你
    2020-12-10 18:48

    Every computing is in same statement for single statement we dont need {} but also in this statement ; (sentence terminator is used in the end) it means next statement does not belong to for statement. Whatever logic is it is in same for statement.

      for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    
    
      for(var j, x, i = o.length;// Initialization
      i;// Work until i is zero
     j = parseInt(Math.random() * i),
      x = o[--i], o[i] = o[j], o[j] = x);//Here he is    computing his logic
    

提交回复
热议问题