Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yitonghe00/0c31801e06a1cc0e372609dfaf68c9c1 to your computer and use it in GitHub Desktop.
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
// 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