JavaScript Shuffling colors in an array to 5 divs jQuery shuffle colors array

Newly into the world of development so please be patient if i lack xp. I need to put 5 specific colors (I'm assuming using an array and shuffle function) in my 5 circles randomly.

HTML

<div id="colorBox"> 
    <div class="circle"></div>          
    <div class="circle"></div>
    <div class="circle"></div>
    <div class="circle"></div>          
    <div class="circle"></div>          
</div>

JS

function shuffle(array) {
    var currentIndex = array.length,
        temporaryValue, randomIndex;


    while (0 !== currentIndex) {


        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }

    return array;
}

var colors = ['red', 'blue', 'green', 'black', 'pink'];
shuffle(colors);

$(".circle").css("background", colors);

I just dont understand what is wrong with my code. please help!

Answer:1

Let's think about this for a second. Let's say your first randomIndex comes from an array.length of 5. The result comes out to be randomIndex is equal to 2. Once the currentIndex is reduced by a value of 1, the next selection size is out of 4. What happens if you get a randomIndex of 2 again? You have overwritten your first CSS styling. What you need to be doing is removing the color from the array after you use it:

function shuffle(array) {

  var randomIndex;
  var currentIndex = 0;

  while (0 !== array.length) {

    randomIndex = Math.floor(Math.random() * array.length);
    var arrayColor = array[randomIndex];

    if (randomIndex > -1) //this cond. statement will remove the array element
     {
      array.splice(randomIndex,1);                 
     }

    $(".circle").eq(currentIndex).css("background", arrayColor); // Set each circle starting at element zero with the "eq" method
    currentIndex++;
 }

}

var colors = ['red', 'blue', 'green', 'black', 'pink'];
shuffle(colors);

See the JSFiddle.

Ultimately what I have done is used a combination of array.splice as well as include the CSS styling of the JQuery WITHIN the loop. This solves the issue highlighted by Anik. If you plan on setting the styling of each circle, then you need to do it within a looping statement/method.

Answer:2

This will do:

Javavscript

var colors = shuffle(['red', 'blue', 'green', 'black', 'pink']);
$(".circle").each(function(index) {
  $(this).css("background", colors[index]);
});

function shuffle(arr) {
  for (i = arr.length - 1; i > 0; i--) {
      var j = Math.floor(Math.random() * (i + 1));
      var temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
  }
  return arr;
}

HTML

<div id="colorBox">
    <div class="circle">&nbsp;</div>
    <div class="circle">&nbsp;</div>
    <div class="circle">&nbsp;</div>
    <div class="circle">&nbsp;</div>
    <div class="circle">&nbsp;</div>
</div>
Answer:3

I'm working on asp.net mvc and using Kendo UI grid control for data retrieving, using these functionality for another application working fine problem not in code. All JavaScript and Kendo grid ui ...

I'm working on asp.net mvc and using Kendo UI grid control for data retrieving, using these functionality for another application working fine problem not in code. All JavaScript and Kendo grid ui ...

I can do this, simply doing it in for loop, but I need to do it without using loop. Is there any way to make it possible? Below I am writing some of my code: function submitFormArchive(Status) { ...

I can do this, simply doing it in for loop, but I need to do it without using loop. Is there any way to make it possible? Below I am writing some of my code: function submitFormArchive(Status) { ...

First time asker. Trying to make a hangman game. When I get keyboard input and match it to a letter in the word, my alert pops. But the execution of the jquery code to show the nth span element ...

First time asker. Trying to make a hangman game. When I get keyboard input and match it to a letter in the word, my alert pops. But the execution of the jquery code to show the nth span element ...

  1. show div jquery
  2. show div jquery animation
  3. show div jquery not working
  4. show div jquery onclick
  5. show div jquery fade
  6. show div jquery hide
  7. show div jquery hover
  8. show div jquery radio button
  9. show div jquery toggle
  10. show hide element jquery
  11. show hidden element jquery
  12. show html element jquery
  13. show next element jquery
  14. show clicked element jquery
  15. show element using jquery
  16. show child element jquery
  17. onclick show element jquery
  18. show element scroll jquery
  19. show first element jquery
  20. show element id jquery

If I use ''.split(',') I get [''] instead of an empty array []. How do I make sure that an empty string always returns me an empty array?

If I use ''.split(',') I get [''] instead of an empty array []. How do I make sure that an empty string always returns me an empty array?

  1. split empty string python
  2. split empty string java
  3. split empty string c#
  4. split empty string javascript
  5. split empty string
  6. split empty string js
  7. split empty string vb.net
  8. split empty string ruby
  9. split empty string perl
  10. split empty string result
  11. split empty string to array
  12. split string empty space c#
  13. split string empty space python
  14. split string empty space java
  15. split string empty values
  16. boost split empty string
  17. split without empty string python
  18. scala split empty string
  19. groovy split empty string
  20. kotlin split empty string