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;
}
}
if(checkColors){is always true.shuffleArray()look like?shuffleArrayis resulting inundefined, which is getting assigned tocheckColors. It's probably missing areturn xyz, but since you haven't shown it...if (checkColors)is just fine, but as Jonas pointed out above, in the given code it'll always be true. (Also:typeofis an operator, not a function. No more need to put its operand in()than any other operator.)