I've been practising JS/ECMAScript lately with examples that I find on the net and while trying one about reversing arrays, I've hit a really strange problem. The code that I put below is complete but half-working.
"use strict";
function reverseArray(theArray){
let tempArray = [];
if(theArray && theArray.length > 0){
console.log('Here works!');
while(theArray.length > 0){
tempArray.unshift(theArray.shift());
}
}
return tempArray;
}
function reverseArrayInPlace(theArray){
console.log(theArray);
if(theArray && theArray.length > 0){
console.log('Should go through here!');
for(let i = 0; i < Math.floor(theArray.length/2); i++){
let temp = theArray[i];
theArray[i] = theArray[theArray.length - 1 - i];
theArray[theArray.length -1 - i] = temp;
}
}
return theArray;
}
let firstSacrifice = ['I','n','v','e','r','t','M','E'];
let secondSacrifice = ['I','n','v','e','r','t','M','E','A','g','a','i','N'];
console.log(firstSacrifice);
console.log('InvertME >>', reverseArray(firstSacrifice));
console.log('InvertME >>', reverseArrayInPlace(firstSacrifice));
console.log('\n');
console.log(secondSacrifice);
console.log('InvertMEAgaiN >>', reverseArray(secondSacrifice));
console.log('InvertMEAgaiN >>', reverseArrayInPlace(secondSacrifice));
The first function, reverseArray, works as expected: it copies the given array into another. The problem comes with the second function, reverseArrayInPlace. In theory, it should do the reversal in the same given array it receives but, strangely, the parameter theArray reaches that function empty []. Therefore, no reversal done with that function at all.
I'm testing the code on a shell with node (v6.10), but in this case I also executed it in the consoles of firefox, chrome and edge with the very same result. For some reason that I cannot fathom (and as usual with this kind of silly errors, I've been trying for a good few hours), the parameter theArray is empty on arrival and, of course, no errors or warnings are given at all.
By the way, I found the exercise proposed in this tutorial about Javascript. I know it's a bit old (and yes, I've already read the docs about JS on MDN) but the exercises proposed there are interesting enough for practice's sake.
Oh, and click here if you want to fiddle with this code, but remember to open the console to see the log entries!
Thanks!