Javascript 与上一个不同的jQuery随机数组数

Javascript 与上一个不同的jQuery随机数组数,javascript,jquery,while-loop,Javascript,Jquery,While Loop,我将一个随机的背景颜色(从3个调色板)应用到页面的不同部分。但是,我想确保同一颜色不会连续出现两次 我认为做一点,而循环会起作用,但从外观上看它不太合适 var colours = new Array('#FF5A5A', '#FFBE0D', '#00DDB8'); var divs = $('.row'); var last; var next; // for each section divs.each(function(i){ // get a random col

我将一个随机的背景颜色(从3个调色板)应用到页面的不同部分。但是,我想确保同一颜色不会连续出现两次

我认为做一点
,而
循环会起作用,但从外观上看它不太合适

var colours = new Array('#FF5A5A', '#FFBE0D', '#00DDB8');    

var divs = $('.row');
var last;
var next;

// for each section
divs.each(function(i){

    // get a random colour
    do {

        next = Math.floor(Math.random()*3);

        // if it's the same as the last one, try again!
        } while( next === last ) {

            next = Math.floor(Math.random()*3);

        }

        // when it's different to the last one, set it
        $(this).css('background-color', colours[next] );

        // tell it this is the last one now
        next = last;

});

有什么想法吗?

这是一种语法错误-您无法决定是要执行while循环还是正常while循环?你所说的将被解释为一个简单的:

这将正确计算一个与上一个不同的数字,但随后将用一个新的(不受限制的)随机数覆盖它。此外,赋值
next=last做的与您想要的相反

因此,将脚本更改为

do {
    next = Math.floor(Math.random()*3);
} while( next === last ) // if it's the same as the last one, try again!

// tell it this is the last one now
last = next;

// now that we've made sure it's different from the last one, set it
$(this).css('background-color', colours[next] );

在函数之前定义next和last会更好

var next=last=Math.floor(Math.random()*3);

$divs.each(function(i){        
    do {
        next = Math.floor(Math.random()*3);
    } while( next === last );  
    $(this).css('background-color', colours[next] );
    next = last;
});

修订版-(因为我觉得自己能接受挑战!)


希望这有帮助!如果你愿意的话,我很乐意一场一场地给你

使用一点数组魔法,不需要内部循环()-


}

为什么不使用do while循环?同样的事情,但语法更短。那么为什么不使用更短的语法:-)do while和while循环之间的简单区别是do while执行代码然后检查语句,而checks语句然后执行代码。对于这个语句,在前面执行代码将是更好的方法。那么,为什么您的答案不使用更好的方法呢?很好,尽管在
used
:-)中,数组并不是只需要一个元素
var next=last=Math.floor(Math.random()*3);

$divs.each(function(i){        
    do {
        next = Math.floor(Math.random()*3);
    } while( next === last );  
    $(this).css('background-color', colours[next] );
    next = last;
});
var last, colours = ['#ff5a5a', '#ffbe0d', '#00ddb8'];    

$('.row').each(function() {
  var color = colours.splice(~~(Math.random()*colours.length), 1)[0];
  $(this).css('background-color', color);   
  last && colours.push(last), last = color;
});
var colours = ['#ff5a5a', '#ffbe0d', '#00ddb8'];    
var used = [];

// for each section
$('.row').each(function(i){

  var color = colours.splice(~~(Math.random()*colours.length), 1)[0];
  $(this).css('background-color', color);

  used.push(color);

  if(used.length > 1) {
    colours.push(used.shift());
  }
});
{
var colours     = ['#FF5A5A', '#FFBE0D', '#00DDB8'],
    divs        = $('.row'),
    coloursSize = colours.length,
    last,
    next;


divs.each(function(i){

// get a random colour
    do {
            next = Math.floor( Math.random() * coloursSize );
            // if it's the same as the last one, try again!
        } while( next === last ) 
        // when it's different to the last one, set it
        $(this).css('background-color', colours[next] );
        // tell it this is the last one now
        last = next;

});