Skip to content
Advertisement

Swap in an array the places which were randomly selected

I have a one-dimensional array [1,2,3,4] and I want to swap two random calculated positions, for example I get position 1 and 4. I want to have a new array that looks like [4,2,3,1]. Does anyone have an idea how to program this?

JavaScript

Advertisement

Answer

Change positions is easy, thought it seems there’s a couple of bugs in your code.

JavaScript
Advertisement