Created
April 12, 2020 01:15
-
-
Save yitonghe00/0c31801e06a1cc0e372609dfaf68c9c1 to your computer and use it in GitHub Desktop.
Excises of Eloquent JavaScript 4th edition. Chapter 04 Problem 02. https://eloquentjavascript.net/04_data.html#i_6xTmjj4Rf5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your code here. | |
const reverseArray = (arr) => { | |
const ans = []; | |
for (let i = arr.length - 1; i >= 0; i--) { | |
ans.push(arr[i]); | |
} | |
return ans; | |
} | |
const reverseArrayInPlace = (arr) => { | |
for (let i = 0, j = arr.length - 1; i < j; i++, j--) { | |
let temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
} | |
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment