Guest User

Untitled

a guest
Feb 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. function reverseArray(array) {
  2. result = [];
  3. for (let item of array) {
  4. result.unshift(item);
  5. }
  6. return result;
  7. }
  8.  
  9. function reverseArrayInPlace(array) {
  10. let len = array.length;
  11. for (let i = 0; i < Math.floor(len/2); i++) {
  12. console.log(i, len-i-1, array[i], array[len-i-1], array);
  13. let swap = array[i];
  14. array[i] = array[len-i-1];
  15. array[len-i-1] = swap;
  16. }
  17. return array;
  18. }
  19.  
  20. console.log(reverseArray(["A", "B", "C"]));
  21. // → ["C", "B", "A"];
  22. let arrayValue = [1, 2, 3, 4, 5];
  23. reverseArrayInPlace(arrayValue);
  24. console.log(arrayValue);
  25. // → [5, 4, 3, 2, 1]
  26. let arrayValue2 = [1, 2, 3, 4];
  27. reverseArrayInPlace(arrayValue2);
  28. console.log(arrayValue2);
  29. // → [4, 3, 2, 1]
Add Comment
Please, Sign In to add comment