Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:54:07+00:00 2026-06-10T15:54:07+00:00

i’ve got an array, and i want to shuffle those according to a certain

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:54:08+00:00Added an answer on June 10, 2026 at 3:54 pm

    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

    var nums = [1,2,3]; // this is your array. it can have any length you want
    // this is one line that does the magic
    nums.push(nums.shift()); // to the left
    nums.unshift(nums.pop()); // to the right
    

    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.

    function rotateArr(arr,pointers,dir) {
        var narr = arr; // create a local copy we can use it
        var savevalue;
        if (dir == 1) { // rotate to the right
            savevalue = narr[pointers[pointers.length-1]]; // save the last value
            for(var i=pointers.length-2;i>=0;i--) {
                narr[pointers[i+1]] = narr[pointers[i]];
            }
            narr[pointers[0]] = savevalue; // set the first value
        } else { // rotate to the left
            savevalue = narr[pointers[0]]; // save the first value
            for(var i=0;i<pointers.length-1;i++) {
                narr[pointers[i]] = narr[pointers[i+1]];
            }
            narr[pointers[pointers.length-1]] = savevalue; // set the last value
        }
        return narr;
    }
    // arr is the array of faces
    // pointers is the elements which you want to rotate (an array)
    // dir is either 1 or -1
    

    you can execute this function with

    nums = rotateArr(nums,[pointers],[1,-1]);
    // just some examples
    nums = rotateArr(nums,[0,1,2,5],1);
    nums = rotateArr(nums,[3,6,1,4],-1);
    

    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.

    Object.defineProperty(Object.prototype, "rotateArr", { value: function(pointers,dir) {
        var savevalue;
        if (dir == 1) { // rotate to the right
            savevalue = this[pointers[pointers.length-1]]; // save the last value
            for(var i=pointers.length-2;i>=0;i--) {
                this[pointers[i+1]] = this[pointers[i]];
            }
            this[pointers[0]] = savevalue;
        } else { // rotate to the left
            savevalue = this[pointers[0]]; // save the last value
            for(var i=0;i<pointers.length-1;i++) {
                this[pointers[i]] = this[pointers[i+1]];
            }
            this[pointers[pointers.length-1]] = savevalue;
        }
    }, enumerable : false});
    

    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

    nums.rotate([pointers],dir);
    

    http://jsfiddle.net/w2jGr/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.