1

I am using the code below to get a list of colours used by 3 divs. If this div has a color set, then it should be pushed into the array checkColors.

var checkColors = [];

$("div[style]").each(function(){
    getColor = $(this).css("color");
    if(getColor){
        checkColors.push(getColor);
    }
});

if(checkColors){
    checkColors = shuffleArray(checkColors);
    if(checkColors.length > 0){
        if(checkColors[0]){
            backgroundColor = checkColors[0];
        }
        if(checkColors[1]){
            primaryColor = checkColors[1];
        }
        if(checkColors[2]){
            secondaryColor = checkColors[2];
        }
    }  
}

However, if(checkColors.length > 0){ gives me a Uncaught TypeError: Cannot read property 'length' of undefined error which I don't understand since I have an If statement to check whether or not the array is undefined.

Here is the shuffleArray() function:

    function shuffleArray(array) {
        for (var i = array.length - 1; i > 0; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }
9
  • 1
    if(checkColors){ is always true. Commented Mar 29, 2018 at 16:53
  • 2
    how does your shuffleArray() look like? Commented Mar 29, 2018 at 16:54
  • 3
    Clearly, calling shuffleArray is resulting in undefined, which is getting assigned to checkColors. It's probably missing a return xyz, but since you haven't shown it... Commented Mar 29, 2018 at 16:55
  • 1
    @scrappedcola: if (checkColors) is just fine, but as Jonas pointed out above, in the given code it'll always be true. (Also: typeof is an operator, not a function. No more need to put its operand in () than any other operator.) Commented Mar 29, 2018 at 16:56
  • 1
    Well, indeed there is no "return" in shuffleArray function, the shuffle array mutates the array and does return nothing. Commented Mar 29, 2018 at 16:57

2 Answers 2

4

I guess that

 shuffleArray(checkColors)

mutates the array instead of returning a new one. Therefore

 checkColors = shuffleArray(checkColors)

will set shuffleArray to undefined.

Sign up to request clarification or add additional context in comments.

1 Comment

Stop upvoting. I dont deserve reputation for this.
0

You are already manipulating the array inside shuffleArray, do not assign it again to checkColors.

Below code change should work -

// checkColors = shuffleArray(checkColors);
// replace above line with
shuffleArray(checkColors);

Otherwise you need to return an array from shuffleArray if you are assigning it to checkColors.

function shuffleArray(array) {
   // your code
   // after for loop
   return array;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.