Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Tony Nguyen
24,934 PointsSwapping numbers in an Array.
I have a program algorithm that I am stuck on.
I'm suppose to create a program that swaps numbers in a given array.
Let's say I were to put an array in my function's argument like this:
[2, 3, 4, 5, 6];
I have to write a function that would return the array like this:
[6, 3, 4, 5, 2];
So I would have to basically swap the 6 and the 2. However please note that however bigger or smaller the array is, this same logic would have to apply to for example [1, 2, 3] to [3, 2, 1]. Or [9, 3, 5, 2, 1, 6] to [6, 3, 5, 2, 1, 9].
Thank you in advance!
3 Answers
Tony Nguyen
24,934 PointsWhat I wanted to initially do was to do it without using helping JavaScript methods. Here was the answer I was looking for:
var x = [2, 3, 5, 7, 6];
function swap(arr) {
var temp = arr[0];
arr[0] = arr[arr.length - 1];
arr[arr.length] = temp;
return arr;
}
console.log(swap(x));
Ted Sumner
Courses Plus Student 17,967 PointsIt has been long enough since I finished the JavaScript course that I don't remember the exact code, but the logic I would use is: store first number to swap in variable. store second number in variable. delete first and second numbers from array. insert second number in first space. insert first number in second space.
There may be an easier way to do this, but I don't know it.
Tony Nguyen
24,934 PointsThis isn't in any of the courses, I was hoping that the Treehouse forum could help me with another algorithm problem that I am trying to solve. However I do agree what you are saying about the logic and I have already thought of this. I am just wanting to see the code that had to be perform to do this.
Ted Sumner
Courses Plus Student 17,967 PointsDo you have to be able to swap any two numbers or just the first and last? I could make a run at it. It would get me back into it, but it would take a while for me to come up with it.
Tony Nguyen
24,934 PointsYes, even if the array is bigger or smaller the same logic would have to apply. For example [1, 2, 3] to [3, 2, 1]. Or [9, 3, 5, 2, 1, 6] to [6, 3, 5, 2, 1, 9].
Wayne Chaw
8,608 Pointsvar array = [2, 3, 4, 5, 6];
function swap (array){
var newArray = array.slice(0);
var temp = newArray[0];
newArray[0] = array[array.length-1];
newArray[array.length-1] = temp;
return newArray;
}
swap(array); //return [ 6, 3, 4, 5, 2 ]
let me know if you need clarifications!
Tony Nguyen
24,934 PointsGreat! Is there a way to do it without the slice method?
Wayne Chaw
8,608 PointsI used the slice method to make a copy of the passed in array. This way, a new array is returned by the function. If you do not create a copy of the array, you will modify the passed in array.
Wayne Chaw
8,608 PointsWayne Chaw
8,608 Pointsjust keep in mind this would modify the original array. if you dont want to modify the original array but create a new one, u have to create a copy first