0

I'm having trouble in figuring out what JavaScript code would work in swapping two values (numbers in this case) in an array.

The function (referred to as move) will swap the position of the value that you clicked with the position of "". The array values are displayed on the page and the function initiates when you click on any number (via onclick).

boxArray = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",""]

function move() {

}
6
  • Can you please post the html and other code as well? If you could create a JSfiddle that would be optimal. Commented Dec 30, 2013 at 21:03
  • 3
    Those aren't numbers. They're strings. More importantly, if you had done even the most minimal research, you would have found that the answer to your question is on StackOverflow already. Commented Dec 30, 2013 at 21:04
  • try to use functions: splice and indexOf Commented Dec 30, 2013 at 21:04
  • 1
    This problem looks like the beginning of one of those sliding tile puzzle games. Commented Dec 30, 2013 at 21:05
  • "The array values are displayed on the page and the function initiates when you click on any number ("onclick")." So is it the DOM elements that should swap? Or the Array members? Or both? We need more info before a reasonable answer can be given. Commented Dec 30, 2013 at 21:06

3 Answers 3

3

To swap two values, use some logic, store one value in a temporary variable, set the first to the second, and the second to the temporary variable :

function swap(array, index1, index2) {
    var temp = array[index1];

    array[index1] = array[index2];
    array[index2] = temp;
}

It's really that easy

FIDDLE

Sign up to request clarification or add additional context in comments.

12 Comments

Simple and to the point.
@tymeJV - Thanks, there are fancier ways to do it, but I don't think it can be any more readable or simpler to understand than this.
Nope...this is basically the exact code that was on a whiteboard in class when I learned it :)
@tymeJV: Seems to me like there's quite a bit more to the question than this answers. Like mapping to clicks on DOM elements, and keeping track of the position of what appears to be a placeholder member of the array.
That's good for the general case, but in the OP one of the values is always '', there is no need for the temporary variable. :-)
|
0

Maybe this (here's the jsfiddle):

<button>1</button><button>2</button>

$('button').click(function() {
    var val = $(this).html();

    swap(val);
});

var boxArray = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",""]

function swap(el) {
    var blankIndex = boxArray.indexOf(""),
        elIndex = boxArray.indexOf(el);

    boxArray[blankIndex] = el;
    boxArray[elIndex] = "";

    console.log(boxArray);
}

1 Comment

+1 for the gratuitous use of a library? ;-)
0

I can't tell precisely what you're trying to do, but I believe I get the gist of it. array.splice is the function you're likely looking for:

function move(arr, from, to) {
    if (from >= arr.length || to >= arr.length) return;
    var y = arr.splice(from, 1)[0];
    arr.splice(to, 0, y);
}

var x = [1,2,3];
move(x, 0, 2); // moves the value at position 0 to position 2: [2,3,1]

Comments