Messed up point counter from trying to sort random images

40 views Asked by At

I have a list that contains 8 images on total. From index value 0-3 are images of recyclable items, and from index value 4-7 are images of compost items. I have 2 images of bins on the UI: a picture of a recycle bin and a picture of a compost bin. I have another blank image on the screen that presents a random image from the list of 8 images. I'm using an if-else statement when you click on either of the bins to determine if the random image matches with the bins. For example, if the random image is list[0], it would be a recyclable item. Thus, if the user clicks on the recycle bin, it should add a point. However, if the random image is list[0] and the user clicks on the compost bin, it should subtract a point. My if-else statement is not working accordingly. Here's the full chunk of code. 1 Heres a picture of the UI. 2

1

There are 1 answers

4
Cengiz On

You should separately create a randomTrash variable and use it in the if statement to check. Like so;

var randomTrash = trash[randomNumber(0,7)];
onEvent("recycle","click", function(){

if(randomTrash == trash[0] ...){
  
  points = points + 1;
  ...
}
})