Exercise 4.2
Reversing an Array
function reverseArray(array){
var result =[];
for(var i=array.length-1; i >= 0; i=i-1){
result.push(array[i]);
}
return result;
}
function reverseArrayInPlace(array){
for(var i =0 ; i< Math.floor(array.length/2);i=i+1){
var old = array[i];
array [i] = array[array.length -1-i];
array[array.length-1-i]=old;
}
return array;
}
console.log(reverseArray(["A", "B", "C"]));
// → ["C", "B", "A"];
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]
To open the JavaScript console, press F12 or on MAC press COMMAND-OPTION-I.