You are given an array with N length. Please develop the function reverseArray to reverse the array
E.g. Input: [1, 2, 3, 4, 5] Output: 5 4 3 2 1
My attempt is as follows:
function printArray (inputArray){
for(let i = 0; i < inputArray.length; i++){
console.log(inputArray[i]);
}
}
function reverseArray (inputArray){
for (var i = inputArray.length - 1; i >= 0; i--){
inputArray.push(inputArray[i]);
}
printArray(inputArray);
}
reverseArray([1, 2, 3, 4, 5]);
But it turns out to be as follows: 1 2 3 4 5 5 4 3 2 1
Can anyone teach me how to solve, i have been struggling with this question for 2 days