So you need swap logic? You should also give the boxes some kind of ID or like an initial order number. When you select the first box, you can store its value temporarily to a local variable and also save the ID of the box. Then when you find the end box, set that box's value to the local variable value, then set a local variable to the end box's value, pick the first box based on that stored ID and set that box's value to the second local variable's value. Or you can do similar logic but with functions and pass through picking values as parameters.