1

Guys i want to get 2 random colors and make background-color for my 4 divs from those 2 selected colors.

What i want is how can i make sure each that 2 colors are used as background-color 2 times.

(In my code sometimes i see one random color as background-color 3 times.)

JSFIDDLE

$(function() {
  function getRandomArrayElements(arr, count) {
    var shuffled = arr.slice(0),
      i = arr.length,
      min = i - count,
      temp, index;
    while (i-- > min) {
      index = Math.floor((i + 1) * Math.random());
      temp = shuffled[index];
      shuffled[index] = shuffled[i];
      shuffled[i] = temp;
    }
    return shuffled.slice(min);
  }

  var randomColor1 = '#' + (Math.random() * 0xFFFFFF << 0).toString(16);
  var randomColor2 = '#' + (Math.random() * 0xFFFFFF << 0).toString(16);

  var colors = [randomColor1, randomColor2];


  $(".first").css("background-color", getRandomArrayElements(colors, 1));
  $(".second").css("background-color", getRandomArrayElements(colors, 1));
  $(".third").css("background-color", randomColor1);
  $(".fourth").css("background-color", randomColor2);

});
div {
  width: 100px;
  height: 100px;
  border: solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
<div class="fourth"></div>
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • Add two counters to your random function. If a color is chosen, increase the counter. If one of the counters is 2 only return the other color – Jonas Wilms Aug 21 '16 at 12:26

3 Answers3

1

How about this:

function removeRandomElement(colors) {
  return colors.splice(Math.floor(Math.random() * colors.length), 1)[0];
}

var colors = [randomColor1, randomColor1, randomColor2, randomColor2];

$(".first").css("background-color", removeRandomElement(colors));
$(".second").css("background-color", removeRandomElement(colors));
$(".third").css("background-color", removeRandomElement(colors));
$(".fourth").css("background-color", removeRandomElement(colors));
knutesten
  • 594
  • 3
  • 16
0

Even this answer comes a bit later I want to tell you about the conceptual mistake in your code above.

You have mixed 2 random picks and 2 fix picks. So it can happen that 3 colors can be the same.

// Possible picks: color1 | color2
$(".first").css("background-color", getRandomArrayElements(colors, 1));

// Possible picks: color1 | color2
$(".second").css("background-color", getRandomArrayElements(colors, 1));

// Fix pick: color1
$(".third").css("background-color", randomColor1);

// Fix pick: color2
$(".fourth").css("background-color", randomColor2);

This is why it can happen that 3 colors could be the same. So you can either remove the picked colors above (like the previous answer was), or put all 4 colors in an array, shuffle it and pick one by one. (like in the following example)

$(function() {

// Code from this SO question:
// http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript
function shuffle(array) {
    let counter = array.length;

    // While there are elements in the array
    while (counter > 0) {
        // Pick a random index
        let index = Math.floor(Math.random() * counter);

        // Decrease counter by 1
        counter--;

        // And swap the last element with it
        let temp = array[counter];
        array[counter] = array[index];
        array[index] = temp;
    }

    return array;
}
  
  var randomColor1 = '#' + (Math.random() * 0xFFFFFF << 0).toString(16);
  var randomColor2 = '#' + (Math.random() * 0xFFFFFF << 0).toString(16);

  var colors = shuffle([randomColor1, randomColor2, randomColor1, randomColor2]);
  var elements = ['.first','.second','.third','.fourth'];

  elements.forEach(function(selector, index) {
    $(selector).css('background-color', colors[index]);
  });
  

});
div {
  width: 100px;
  height: 100px;
  border: solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
<div class="fourth"></div>
wildhaber
  • 1,631
  • 1
  • 18
  • 23
0

function getRandomArrayElements(arr, count) {
    var shuffled = arr.slice(0),
      i = arr.length,
      min = i - count,
      temp, index,color;
    while (i-- > min) {
      index = Math.floor((i + 1) * Math.random());
      temp = shuffled[index];
      shuffled[index] = shuffled[i];
      shuffled[i] = temp;
   color = shuffled.slice(min);
   if(arr.length == getRandomArrayElements["time"].length)
   {
    break;
   }
   if(getRandomArrayElements["time"][color] != undefined)
   {
    i++;
   }
   else
   {
  getRandomArrayElements["time"][color] = "1"  
   }
    }
 
    return color;
  }
 getRandomArrayElements["time"] = [];