I have three colours in an array. Initally I was asked to create a function that would simply take the first one and append it to the end or do the reverse, so I created this function:

```
// Get our colours
var colours = scope.colours;

// Create our function
scope.swap = function (opposite) {

    // If we want to go in the opposite direction
    if (opposite) {

        // Shift the array in reverse
        colours.unshift(colours.pop());

    // Else
    } else {

        // Shift the array
        colours.push(colours.shift());
    }
};

```

this worked fine.
Now my client has asked me for something more complicated and I am just want to see if there is an easy way to do this.

They have given me a table a bit like this:

---

| C1 | C2 | C3 |
----------------

| C2 | C1 | C3 |
----------------

| C1 | C3 | C2 |
----------------

| C2 | C3 | C1 |
----------------

So basically, 

* **C3** can only be in position 2 and 3
* **C2 and C1** can be in any position

Before I start messing around, does anyone know of a simple way of doing this?

Possible solution
-----------------

So, after I wrote this, I decided to have a go anyway.
I came up with this:

```
// Get our colours
var colours = scope.colours;

// Create our variables
counter = 2;

// Create our function
scope.swap = function (opposite) {

    switch (counter) {
        case 3:

            // Get our colours
            var colour2 = colours.shift();
            var colour3 = colours.shift();

            // Rearange them
            colours.splice(1, 0, colour2);
            colours.splice(2, 0, colour3);

            // Exit the switch
            break;

        case 1:

            // Get our colours
            var colour2 = colours.shift();
            var colour1 = colours.shift();

            // Rearange them
            colours.splice(0, 0, colour1);
            colours.splice(2, 0, colour2);

            // Exit the switch
            break;
        case 0:

            // Get our colours
            var colour1 = colours.shift();
            var colour3 = colours.shift();

            // Rearange them
            colours.splice(1, 0, colour3);
            colours.splice(2, 0, colour1);

            // Exit the switch
            break;
        default:

            // Remove and store our first array value
            var colour1 = colours.shift();

            // Add the colour into the array in the second position
            colours.splice(1, 0, colour1);

            // Exit the switch
            break;
    }

    // Decrease
    counter--;

    // If our counter is less than 0
    if (counter < 0) {

        // Reset the counter
        counter = 3;
    }
};

```

Because there will never be more than 3 colours I just built in a switch statement and used **shift()** to re-arrange my array.
Now this woks great, but what happens if I want to go in the other direction? i.e. instead of decreasing the counter, I want to be able to increase it too (and reset to 0 when greater than 3).