i’ve got an array, and i want to shuffle those according to a certain pattern
(i’m trying to make a rubics cube in javascript).
I want to assign value2 to value 1 and value 1 to value 3 and value 3 to value 2. I can do that within 4 lines of code, but is there a shorter way?
like:
temp = var3; //make temporary variable
(var3 = var2) = var1;//put var2 in var3 and var3 in var1
var1 = temp;//put var3/temp in var1
i know that it doesn’t work this way, but do you guys know a way it does work?
that would be usefull when cycling 8 variables.
thanks,
Tempestas Ludi.
If you’re dealing with more than 2 variables, it’s always best to use an array, since arrays have built in functions you can use
http://jsfiddle.net/wbKYY/2/
http://jsfiddle.net/wbKYY/4/
Anyway, about your comment. Seeing as the pointers which you will rotate aren’t predetermined, and will vary, it’s probably best to use a function.
A function that will iterate through pointers that you define.
you can execute this function with
Here’s a working example:
http://jsfiddle.net/w2jGr/
However if you prefer to use a prototype function that is just a method of an array, you can define a property and just access it from there.
Modifying the prototype of an object is never recommended, but if you aren’t going for clean code and want usability or readability, this works great as you can call the function with
http://jsfiddle.net/w2jGr/1/