/**
* Reversing an array
(without Array.reverse())
*
* 1)
* Create a function, reverseArray(arr), that expects a single array as an argument
* It will return a new array, with all the values in reverse order
*/
function reverseArray(arr) {
var newArr = [];
for (var i = 0; i < arr.length; i += 1) {
newArr.unshift(arr[i]);
}
return newArr;
}
/* Not working
function reverseArrayInPlace(arr) {
for (var i = arr.length; i <= 0; i -= 1) {
console.log("i = " + i);
arr.push(arr[i]);
var iVal = arr.splice(i, 1);
console.log(iVal);
}
console.log(arr);
}*/
//console.log(reverseArray(["A", "B", "C"]));
// → ["C", "B", "A"];
/*
* 2)
* Upgrade the reverseArray function so that when you pass a value
* that is not an array it will throw an exception
*/
console.log(reverseArrayInPlace(["A", "B", "C"]));
/*
* 3) Bonus
* Create a new function, reverseArrayInPlace(arr)
* which does the same but does NOT use two arrays during the reversal process.
*/
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.