I have made a fiddle:
I’m trying to get the code to a point, that it never ever shows the same two items at once or when one item is clicked, it never pulls back that same one from the array, just seem to be struggling a little with the logic and the indexOf method is behaving oddly.
var justAdded = [];
justAdded['first'] = 0;
justAdded['second'] = 1;
newHtml = returnRandom().split('|');
justAdded[e.id] = parseInt(newHtml[0], 10);
if(justAdded.indexOf(parseInt(newHtml[0], 10)) == -1){
e.style.opacity = 0;
e.innerHTML = newHtml[1];
e.style.opacity = 1;
e.setAttribute('data-id', newHtml[0]);
} else {
uniq(clickedEl);
}
var returnRandom = function(){
return options[Math.floor(Math.random() * options.length)]
};
e.id will be equal to ‘first’ or ‘second’. returnRandom() will grab a random value from the options array:
var options = [
'0|Flash',
'1|Internet Explorer',
'2|Java',
'3|!important'
];
You seem to be adding non-numeric properties to an Array.
The
indexOf()method is intended to work on and return numeric properties.Furthermore…
the
processAnswerhandler passes the ID of the element toaddHtml… like “first”, “second”addHtmluses that ID string to grab the very same element stored in a variable with the same name, and then passes that element touniquniqadds a property to thejustAddedArray, with the key being the ID of the element.You keep switching between passing elements and their IDs, and using the one to get the other in every next function. All that is to say that your code seems terribly disorganized, and I think you just need to start from scratch and rethink your code.
Also, what the heck is with this?…
Why are you hardcoding indices into strings in the Array? Arrays are ordered lists. They already have the indices taken care of.