+ 1
Is there a method to switch an array content position with another one in the same array?
badically let's say I have an array like this: myArray = [1,0,0,0]; and I want it to switch the position of the 1 with one of the zeroes: [0,1,0,0] is there a method or function I can use or it's only manual.
4 Answers
+ 2
Or it could be as a function:
https://code.sololearn.com/WuE96c4HGuur/?ref=app
+ 3
It may be complex, but there is a short way:
[myArray[0], myArray[1]] = [myArray[1], myArray[0]];
+ 2
I fixed my code. It's now compatible with all browsers.
0
Thanks a lot but I couldn't run the code, probably my browser is too old. But your first answer mayor be useful.
Edit:managed to get your code running there were some extra [ ] in the if statement and had to change let for a variable.