Swap values in an array
Swap values in an array
How can I write this in a concise way? Basically, I would like the values in 'middleArray' to change base on the value of the variable 'multiplier'. 'multiplier' is determined by the user and will have a value between 2 and 5 inclusive, so if the user selects say '3' then I need the values in the array to change/be updated accordingly. I've looked at the .map() functionality but am not really sure how to apply it in this instance or if it even is the right approach!
- if(multiplier == 2) {
- var middleArray = [2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4];
- }
- else if(multiplier == 3) {
- var middleArray = [3, 6, 9, 2, 5, 8, 1, 4, 7, 0, 3, 6];
- }
- else if(multiplier == 4) {
- var middleArray = [4, 8, 2, 6, 0, 4, 8, 2, 6, 0, 4, 8];
- }
- else {
- var middleArray = [5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0];
- };
Many thanks