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() {

}
cookie monster
  • 10,671
  • 4
  • 31
  • 45
  • Can you please post the html and other code as well? If you could create a JSfiddle that would be optimal. – Mussser Dec 30 '13 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](http://stackoverflow.com/questions/872310/javascript-swap-array-elements). – 15ee8f99-57ff-4f92-890c-b56153 Dec 30 '13 at 21:04
  • try to use functions: splice and indexOf – Igor Benikov Dec 30 '13 at 21:04
  • 1
    This problem looks like the beginning of one of those sliding tile puzzle games. – cHao Dec 30 '13 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. – cookie monster Dec 30 '13 at 21:06
  • And what does *'... with the position of ""'* mean? Is it the Array member with `""`? – cookie monster Dec 30 '13 at 21:10

3 Answers3

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

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Simple and to the point. – tymeJV Dec 30 '13 at 21:08
  • @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. – adeneo Dec 30 '13 at 21:10
  • Nope...this is basically the exact code that was on a whiteboard in class when I learned it :) – tymeJV Dec 30 '13 at 21:11
  • @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. – cookie monster Dec 30 '13 at 21:14
  • 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. :-) – RobG Dec 30 '13 at 21:15
  • @cookiemonster - That's probably true, but there's no explanation or code to show how that is supposed to work, just "this is on the page", so who knows how to answer that correctly ? – adeneo Dec 30 '13 at 21:16
  • @adeneo: So why answer if we don't know how for lack of information? – cookie monster Dec 30 '13 at 21:18
  • @cookiemonster - as far as I can tell, there is one major question, it's in the first line "I'm having trouble in figuring out what JavaScript code would work in swapping two values in an array", and this answers that. How to make it work with clicking something is impossible to answer without seeing the code for that. – adeneo Dec 30 '13 at 21:23
  • This question is a troll. Vague, incomplete, no response from OP, and OP's account is inactive/deleted. Still, there's certainly more to it. – cookie monster Dec 30 '13 at 21:25
  • Doesn't matter to me, I'm not getting the upvotes anyway, and just answered to be helpful. – adeneo Dec 30 '13 at 21:28
  • Yeah, one would have to be quite the tool to care about rep points here. Kind of like the person who suddenly removed an upvote from a answer I gave 4 days ago. Doesn't bother me, but that person must think that points here actually mean something. – cookie monster Dec 30 '13 at 21:35
  • It's nice to get the points when you answer something, but I've reached my limit for today, so upvotes aren't counted anyway, and I don't really care about the rep either. – adeneo Dec 30 '13 at 21:43
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);
}
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
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]
AnalogWeapon
  • 550
  • 1
  • 4
  • 16