You can check for the solutions of the problems by using the following link.
https://eloquentjavascript.net/2nd_edition/code/
Just select the chapter and the according exercise. You can even get a running solution by clicking the button “show the solution”. But at first, you should try to develop a running version on your own. This is very important to get better. By doing this you can compare the solution provided with your own one and also analyse the shortcomings of your solution. You can even run your code by pasting it and clicking on the “run code” button.
Now to the problem of the two different functions to reverse an array. To be honest I didn’t understand the difference of these exercises, too. I implemented only the first version and then I had a look at the two solutions. By looking at the solutions I figured out what the difference was:
First exercise by example without a loop for better understanding:
var oldArray = ["one", "two", "three", "four"];
var newArray = ["undefined", "undefined", "undefined", "undefined"];
newArray[0] = oldArray[3];
newArray[1] = oldArray[2];
newArray[2] = oldArray[1];
newArray[3] = oldArray[0];
console.log(newArray); will get you ["four", "three", "two", "one"].
The idea of the solution is that you create a new array. One array is iterated from start to end, while the other array is iterated from end to start to get the order of the elements reversed. Sure you can also start with an empty second array! So the code above is not considered as good programming style, it is just to show you how the algorithm for reversing the array works. If you do the exercises you will need loops for sure!
Second exercise by example without a loop for better understanding:
In the second exercise you don’t have a second array! Just use the first one!
var oldArray = ["one", "two", "three", "four"];
//You will need an additional variable for saving one value! Sometimes it is called "Dummy" variable.
var saveThisForAMoment = "undefined";
//Swapping the first element with the last element of the array.
saveThisForAMoment = oldArray[0];
oldArray[0] = oldArray[3];
oldArray[3] = saveThisForAMoment;
//Swapping the second element with the second last element of the array.
saveThisForAMoment = oldArray[1];
oldArray[1] = oldArray[2];
oldArray[2] = saveThisForAMoment;
console.log(oldArray); will get you ["four", "three", "two", "one"].
The cool thing about this solution is, that you only go half through the array. You start with swapping the values of the first array element with the last array element.
But what about array with and odd number of elements? Lets look just at the values:
“one” → “five”
“two” → “four”
“three” → “three”
“four” → “two”
“five” → “one”
The element in the middle does not change at all. “three” stays “three”. So you can stop after swapping the values “one” and “two” with “five” and “four”.
I hope this helps! Don’t be disappointed in your abilities. Getting a good programmer really takes some time. And it also took me quite some time to figure things out. By learning the syntax of a programming language you will not become a good programmer at once! It always take some “blood, sweat and tears” like Ivan always says to get more experience and also learn about the pitfalls!