-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreversearr.js
47 lines (34 loc) · 1.09 KB
/
reversearr.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(function(){
// function swap(a,b){
// let temp;
// temp = a;//temp = 5 a=5, b=6
// a = b;//temp = 5,a = 6, b= 6
// b = temp//temp= 5, a =6 ,b =5
// return {
// a:a,
// b:b
// }
function swap (arr,left,right){
let temp;
temp = arr[left];
arr[left] = arr[right];
arr[right] = temp
}
function reverse(arr){
if(arr.lenght == 1){
return arr[0];
}
let leftPointer = 0;
let rightPointer =arr.lenght - 1;
while(leftPointer <= rightPionter){
swap(arr, leftPointer, rightPointer);
// console.log ("After sawp" + arr[leftPointer]+""+[rightPointer])
// swap(arr [leftPointer], arr[rightPointer]);
// console.log ("Before sawp" + arr[leftPointer]+""+[rightPointer])
leftPointer++;
rightPointer--;
}
return arr;
}
console.log(reverse[1,2,3,4,5,6])
})();