I'm a beginner at JS and I am trying to create a random chooser that gives me a word from a list of words for an English lesson. I created a new class that has all of the formatting information (name, image, background color, border color) for when you click on the button to produce a random word. I want to access a specific instance (which is hard coded for the time being but I hope will be user input as I learn more), and then apply each property to the corresponding CSS code to change the image on the screen, and after each click it should register if a word has been chosen already and stop picking it after it has been chosen two times. In essence, I'm not really sure how to approach this issue to begin with. Can I iterate over class instances once I've created them?
I am able to get it to work with an array of objects that I can iterate over and draw from, but I am not able to stop it once it has hit so many times. I've tried pushing values to a new array and then comparing two arrays, but that didn't seem to work. I was imagining that I could just increment a hitCount property in a class and stop it from choosing one that is already at 2.
class Vocab {
constructer(name, image, bgcolor, bordercolor) {
this._name = name;
this._image = image;
this._bgcolor = bgcolor;
this._bordercolor = bordercolor;
this._hitCount = 0;
}
get name() {
return this._name;
}
get image() {
return this._image;
}
get bgcolor() {
return this._bgcolor;
}
get bordercolor() {
return this._bordercolor;
}
incrementHitCount() {
this._hitCount++;
}
}
const running = new Vocab('running', '<img class="verb-pics" src="./images/running.png">', '#fc4e4e', '#9c3333');
I've also tried Vocab as an array of objects and I can get the properties but still not sure how to stop each instance after it's been chosen. With this I can't seem to even access each property when using something like "running.name".
EDIT: The best answer was in comments and I though I should put it here to make it more clear...
doubling up each element in one array, shuffling the array, and then using .pop() was the best answer.