Skip to content
Permalink
bb11685aea
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
26 lines (24 sloc) 983 Bytes
function reverseArray(array) {
empty = [];
for(let rev of array) {
empty.unshift(rev);
}
return empty;
}
function reverseArrayInPlace(array){
for (i = 0; i < Math.floor(array.length / 2); i++){
// loops the array from 0 to the half of the array (rounded to lower number), increases i by one each time.
var old = array[i];
//temporarely stores the current value of the array at the position i in the variable old.
array[i] = array[array.length - 1 - i];
//sets the value of the i position to the value of the last element of the array minus the current i.
array[array.length - 1 - i] = old;
//sets the value of the last element of the array minus the current i to the previous value (stored in the old variable).
}
}
console.log(reverseArray(["A", "B", "C"]));
// → ["C", "B", "A"];
let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]