Hey guys,
I'm just starting out in Javascript and I was trying to do two simple exercises.
The first one takes an array and produces a new inverted one:
function reverseArray(input) {
var newArray=[];
for (var i=input.length-1; i>-1; i--) {
newArray.push(input[i]);
}
return newArray;
}
console.log(reverseArray(["A", "B", "C"]));
// → ["C", "B", "A"];
The second one inverts the variable itself:
function reverseArray(input) {
var newArray=[];
var originalLength=input.length;
for (var i=input.length-1; i>-1; i--) {
input.push(input[i]);
}
for (var i=0; i<originalLength; i++) {
input.shift();
}
return input;
}
var arrayValue = [1, 2, 3, 4, 5, 6, 7];
reverseArray(arrayValue);
console.log(arrayValue);
// [7, 6, 5, 4....
It's working, but isn't there a simple way to just move the array to another variable? If I just take the first exercise and print in the last line input=newarray, the array doesn't move to the input variable.
Thanks a lot in advance guys
Omer
CodingForums...
ما را در سایت CodingForums دنبال می کنید
برچسب : نویسنده : codingforums بازدید : 187 تاريخ : شنبه 28 مرداد 1396 ساعت: 6:05