2
var x1 ='&spades',
    y1 ='&clubs'
    z1 ='&hearts';
var x2 = ' ', y2 = ' ', z2 = ' ';  
var x3 = ' ', y3 = ' ', z3 = ' ';    


var Array = [x1,y1,z1,
             x2,y2,z2,
             x3,y3,z3];

Then I get an input from the user asking them to select a location (ex. x1) store it as input 1, then ask them for another location (ex. x2) and store that as input 2, and I have to swap the locations of the value.

I initially tried something like Array[input1] = Array[input2]

But Array[input1] is undefined even though input1 = x1 because Array[x1] is still undefined.

So how do I make Array[x1] = Array[0] so I can swap the values or is there an easier way?

Sorry if my question is poorly formed.

9
  • For starters, 'Array' is a keyword. Probably not a good idea to use it as a variable name. Next, the indices to your array are 0 through 9. JS has no idea how to map 'x2' to 4. Commented Nov 12, 2018 at 4:22
  • use a variable to temporarily store the value of Array[firstValueToSwap] value and store the Array[secondValueToSwap] in Array[firstValueToSwap] and store the value in temp to Array[secondValueToSwap]. It's the easiest way to swap two values. BTW your question is not that clear. hope this is what you're looking for Commented Nov 12, 2018 at 4:25
  • @CertainPerformance Yes the input is a string. Commented Nov 12, 2018 at 4:28
  • As per my understanding, you have an array with 9 elements in it. Example: [ '&spades', '&clubs', '&hearts', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. Now you want to swap the values at 2 indexes (input1 and input2) for example if the user entered 0 and 2. The new array will be [ '&hearts', '&clubs', '&spades', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. I am writing this comment so that community can understand the question correctly. Commented Nov 12, 2018 at 4:30
  • @JimB. I know this isnt my real code my array is a lot bigger just a simple version, and I know that JS doesnt know how to map x2 to 4 but I was trying to see if there is any way otherwise i have to rewrite my program Commented Nov 12, 2018 at 4:30

2 Answers 2

2

Assuming the inputs are strings ('x1' and 'x2'), you need a way to represent the variable names in your data structure. One way would be to have, rather than an array of strings, an array of objects, containing a label and a value property. Then, just find the indicies of both labels for both inputs, and reassign those indicies.

Also, better not to assign to a variable named Array, because that will shadow the global Array object - name it something else instead, like arr:

const arr = [
  { label: 'x1', value: '&spades' },
  { label: 'y1', value: '&clubs' },
  { label: 'z1', value: '&hearts' }
  // ...
];
// Inputs:
const label1 = 'x1';
const label2 = 'y1';

// Calculate indicies:
const [index1, index2] = [label1, label2].map(
  findLabel => arr.findIndex(({ label }) => label === findLabel)
);
([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);
console.log(arr);

The destructuring line at the end there allows for swapping variables in two positions at once, without having to resort to an intermediate variable:

([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);

If you find that confusing, an alternative is to actually use an intermediate variable:

const orig1 = arr[index1];
arr[index1] = arr[index2];
arr[index2] = orig1;
Sign up to request clarification or add additional context in comments.

2 Comments

I will try this as soon as I get back home, I see what you mean, I used a very very simple version of my code as an example of what I was trying to do I know not to name it array was just example but I will try to implement this method into my own code will let you know how it goes, thank you
Yes, thank you @CertainPerformance, by making my array of objects now allows me to make the connection between the array and users input so now I can switch them, at the bottom I get a bit fuzzy on how the whole switching works, I just used some if statements with an intermediate var because I didnt understand what the bottom half was doing. Though I will try to play around to try and understand it. Thank you for your help.
0

Here's what your code might look like if you use an object:

    let arr = {};
    arr['x1'] = '&spades';
    arr['y1'] = '&clubs';
    arr['z1'] = '&hearts';
    arr['x2'] = ' ';
    arr['y2'] = ' ';
    arr['z2'] = ' ';
    arr['x3'] = ' ';
    arr['y3'] = ' ';
    arr['z3'] = ' ';
    
    console.log(arr['z1']);
    arr['z1'] = 'foo';
    console.log(arr['z1']);

2 Comments

I see what you mean now CertainPerformace was saying something about the same will try as soon as I can, thank you
Yes, @JimB. you were right the answer was that I had to make an array of objects. Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.