-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversingAnArray.js
More file actions
27 lines (25 loc) · 977 Bytes
/
reversingAnArray.js
File metadata and controls
27 lines (25 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
Arrays have a method reverse , which changes the array by inverting the order
in which its elements appear. For this exercise, write two functions,
reverseArray and reverseArrayInPlace . The first, reverseArray , takes an array
as argument and produces a new array that has the same elements in the inverse
order. The second, reverseArrayInPlace , does what the reverse method does:
it modifies the array given as argument in order to reverse its in the previous
chapter , which variant do you expect to be useful in more situations? Which
one is more efficient?
*/
function reverseArray(array) {
var newArray = [];
for(var i = array.length - 1; i >= 0; i--) {
newArray.push(array[i]);
}
return newArray;
}
function reverseArrayInPlace(array) {
for (var i = 0; i < Math.floor(array.length / 2); i++) {
var old = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = old;
}
return array;
}